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

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.28    ! frystyk   405:     HTInputSocket *isoc = (HTInputSocket *)calloc(1, sizeof(*isoc));
1.13      timbl     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;
1.25      frystyk   705:     float best_quality = -1e30;                /* Pretty bad! */
                    706:     HTPresentation *pres, *match, *best_match=0, *source_match=0;
1.14      timbl     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.25      frystyk   723:            if  ((pres->rep == rep_in || wild_match(pres->rep, rep_in)) &&
                    724:                (pres->rep_out == rep_out || wild_match(pres->rep_out, rep_out))) {
                    725:                if (pres->quality > best_quality) {
                    726:                    best_match = pres;
                    727:                    best_quality = pres->quality;
1.10      timbl     728:                }
                    729:            }
1.25      frystyk   730:            
                    731:            /* Special case when input format is 'www/source' */ 
1.10      timbl     732:            if (pres->rep == source) {
1.25      frystyk   733:                if (pres->rep_out == rep_out || wild_match(pres->rep_out, rep_out))
1.10      timbl     734:                    source_match = pres;
1.2       timbl     735:            }
                    736:        }
                    737:     }
1.14      timbl     738:     
1.25      frystyk   739:     match = best_match ? best_match :
                    740:            source_match ? source_match : 
                    741:            NULL;
1.14      timbl     742:     if (match) return (*match->converter)(
1.25      frystyk   743:        request, match->command, rep_in, rep_out,
                    744:        request->output_stream);
1.2       timbl     745:     return NULL;
                    746: }
                    747:        
                    748: 
                    749: /*             Find the cost of a filter stack
                    750: **             -------------------------------
                    751: **
                    752: **     Must return the cost of the same stack which StreamStack would set up.
                    753: **
                    754: ** On entry,
                    755: **     length  The size of the data to be converted
                    756: */
1.12      timbl     757: PUBLIC float HTStackValue ARGS5(
1.14      timbl     758:        HTList *,               theseConversions,
1.10      timbl     759:        HTFormat,               rep_in,
1.2       timbl     760:        HTFormat,               rep_out,
                    761:        float,                  initial_value,
                    762:        long int,               length)
                    763: {
1.14      timbl     764:     int which_list;
                    765:     HTList* conversion[2];
                    766:     
1.2       timbl     767:     if (TRACE) fprintf(stderr,
                    768:        "HTFormat: Evaluating stream stack for %s worth %.3f to %s\n",
1.10      timbl     769:        HTAtom_name(rep_in),    initial_value,
1.2       timbl     770:        HTAtom_name(rep_out));
                    771:                
                    772:     if (rep_out == WWW_SOURCE ||
1.10      timbl     773:        rep_out == rep_in) return 0.0;
1.2       timbl     774: 
1.12      timbl     775:  /*   if (!HTPresentations) HTFormatInit();     set up the list */
1.2       timbl     776:     
1.14      timbl     777:     conversion[0] = theseConversions;
                    778:     conversion[1] = HTConversions;
                    779:     
                    780:     for(which_list = 0; which_list<2; which_list++)
                    781:      if (conversion[which_list]) {
1.15      luotonen  782:         HTList * cur = conversion[which_list];
1.2       timbl     783:        HTPresentation * pres;
1.15      luotonen  784:        while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
                    785:            if (pres->rep == rep_in &&
1.17      luotonen  786:                (pres->rep_out == rep_out || wild_match(pres->rep_out, rep_out))) {
1.2       timbl     787:                float value = initial_value * pres->quality;
                    788:                if (HTMaxSecs != 0.0)
1.15      luotonen  789:                    value = value - (length*pres->secs_per_byte + pres->secs)
1.2       timbl     790:                                         /HTMaxSecs;
                    791:                return value;
                    792:            }
                    793:        }
                    794:     }
                    795:     
                    796:     return -1e30;              /* Really bad */
1.17      luotonen  797: }
                    798: 
                    799: 
1.2       timbl     800: 
1.1       timbl     801: 
1.2       timbl     802: /*     Push data from a socket down a stream
                    803: **     -------------------------------------
1.1       timbl     804: **
1.2       timbl     805: **   This routine is responsible for creating and PRESENTING any
1.1       timbl     806: **   graphic (or other) objects described by the file.
1.2       timbl     807: **
                    808: **   The file number given is assumed to be a TELNET stream ie containing
                    809: **   CRLF at the end of lines which need to be stripped to LF for unix
                    810: **   when the format is textual.
                    811: **
1.26      luotonen  812: **   RETURNS the number of bytes transferred.
                    813: **
1.1       timbl     814: */
1.26      luotonen  815: PUBLIC int HTCopy ARGS2(
1.2       timbl     816:        int,                    file_number,
                    817:        HTStream*,              sink)
1.1       timbl     818: {
1.2       timbl     819:     HTStreamClass targetClass;    
1.13      timbl     820:     HTInputSocket * isoc;
1.26      luotonen  821:     int cnt = 0;
                    822: 
1.5       timbl     823: /*     Push the data down the stream
1.2       timbl     824: **
                    825: */
                    826:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
1.13      timbl     827:     isoc = HTInputSocket_new(file_number);
1.2       timbl     828:     
                    829:     /* Push binary from socket down sink
1.10      timbl     830:     **
                    831:     **         This operation could be put into a main event loop
1.2       timbl     832:     */
                    833:     for(;;) {
                    834:        int status = NETREAD(
1.13      timbl     835:                file_number, isoc->input_buffer, INPUT_BUFFER_SIZE);
1.2       timbl     836:        if (status <= 0) {
                    837:            if (status == 0) break;
                    838:            if (TRACE) fprintf(stderr,
1.24      luotonen  839:                "HTFormat: Read error, read returns %d with errno=%d\n",
                    840:                status, errno);
1.2       timbl     841:            break;
                    842:        }
1.26      luotonen  843: 
1.8       timbl     844: #ifdef NOT_ASCII
                    845:        {
                    846:            char * p;
1.13      timbl     847:            for(p = isoc->input_buffer; p < isoc->input_buffer+status; p++) {
1.8       timbl     848:                *p = FROMASCII(*p);
                    849:            }
                    850:        }
                    851: #endif
                    852: 
1.13      timbl     853:        (*targetClass.put_block)(sink, isoc->input_buffer, status);
1.26      luotonen  854:        cnt += status;
1.2       timbl     855:     } /* next bufferload */
1.26      luotonen  856: 
1.13      timbl     857:     HTInputSocket_free(isoc);
1.26      luotonen  858: 
                    859:     return cnt;
1.2       timbl     860: }
                    861: 
1.1       timbl     862: 
1.7       secret    863: 
                    864: /*     Push data from a file pointer down a stream
                    865: **     -------------------------------------
                    866: **
                    867: **   This routine is responsible for creating and PRESENTING any
                    868: **   graphic (or other) objects described by the file.
                    869: **
                    870: **
                    871: */
                    872: PUBLIC void HTFileCopy ARGS2(
                    873:        FILE *,                 fp,
                    874:        HTStream*,              sink)
                    875: {
                    876:     HTStreamClass targetClass;    
1.13      timbl     877:     char input_buffer[INPUT_BUFFER_SIZE];
1.7       secret    878:     
                    879: /*     Push the data down the stream
                    880: **
                    881: */
                    882:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
                    883:     
                    884:     /* Push binary from socket down sink
                    885:     */
                    886:     for(;;) {
                    887:        int status = fread(
                    888:               input_buffer, 1, INPUT_BUFFER_SIZE, fp);
                    889:        if (status == 0) { /* EOF or error */
                    890:            if (ferror(fp) == 0) break;
                    891:            if (TRACE) fprintf(stderr,
                    892:                "HTFormat: Read error, read returns %d\n", ferror(fp));
                    893:            break;
                    894:        }
                    895:        (*targetClass.put_block)(sink, input_buffer, status);
1.13      timbl     896:     } /* next bufferload */    
1.7       secret    897: }
                    898: 
                    899: 
                    900: 
                    901: 
1.2       timbl     902: /*     Push data from a socket down a stream STRIPPING CR
                    903: **     --------------------------------------------------
                    904: **
                    905: **   This routine is responsible for creating and PRESENTING any
1.8       timbl     906: **   graphic (or other) objects described by the socket.
1.2       timbl     907: **
                    908: **   The file number given is assumed to be a TELNET stream ie containing
                    909: **   CRLF at the end of lines which need to be stripped to LF for unix
                    910: **   when the format is textual.
                    911: **
1.1       timbl     912: */
1.2       timbl     913: PUBLIC void HTCopyNoCR ARGS2(
                    914:        int,                    file_number,
                    915:        HTStream*,              sink)
                    916: {
1.13      timbl     917:     HTStreamClass targetClass;
                    918:     HTInputSocket * isoc;   
1.1       timbl     919:     
1.2       timbl     920: /*     Push the data, ignoring CRLF, down the stream
                    921: **
                    922: */
                    923:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
                    924: 
                    925: /*     Push text from telnet socket down sink
                    926: **
                    927: **     @@@@@ To push strings could be faster? (especially is we
                    928: **     cheat and don't ignore CR! :-}
                    929: */  
1.13      timbl     930:     isoc = HTInputSocket_new(file_number);
1.2       timbl     931:     for(;;) {
                    932:        char character;
1.13      timbl     933:        character = HTInputSocket_getCharacter(isoc);
1.2       timbl     934:        if (character == (char)EOF) break;
                    935:        (*targetClass.put_character)(sink, character);           
                    936:     }
1.13      timbl     937:     HTInputSocket_free(isoc);
1.2       timbl     938: }
1.1       timbl     939: 
1.2       timbl     940: 
1.7       secret    941: 
1.2       timbl     942: /*     Parse a socket given format and file number
                    943: **
                    944: **   This routine is responsible for creating and PRESENTING any
                    945: **   graphic (or other) objects described by the file.
                    946: **
                    947: **   The file number given is assumed to be a TELNET stream ie containing
                    948: **   CRLF at the end of lines which need to be stripped to LF for unix
                    949: **   when the format is textual.
                    950: **
                    951: */
1.14      timbl     952: 
1.12      timbl     953: PUBLIC int HTParseSocket ARGS3(
1.10      timbl     954:        HTFormat,               rep_in,
1.2       timbl     955:        int,                    file_number,
1.12      timbl     956:        HTRequest *,            request)
1.2       timbl     957: {
                    958:     HTStream * stream;
                    959:     HTStreamClass targetClass;    
1.1       timbl     960: 
1.12      timbl     961:     stream = HTStreamStack(rep_in, request);
1.2       timbl     962:     
                    963:     if (!stream) {
                    964:         char buffer[1024];     /* @@@@@@@@ */
                    965:        sprintf(buffer, "Sorry, can't convert from %s to %s.",
1.12      timbl     966:                HTAtom_name(rep_in), HTAtom_name(request->output_format));
1.3       timbl     967:        if (TRACE) fprintf(stderr, "HTFormat: %s\n", buffer);
1.16      luotonen  968:         return HTLoadError(request, 501, buffer);
1.2       timbl     969:     }
1.1       timbl     970:     
1.3       timbl     971: /*     Push the data, ignoring CRLF if necessary, down the stream
                    972: **
1.2       timbl     973: **
1.3       timbl     974: **   @@  Bug:  This decision ought to be made based on "encoding"
1.9       timbl     975: **   rather than on format.  @@@  When we handle encoding.
1.3       timbl     976: **   The current method smells anyway.
1.2       timbl     977: */
                    978:     targetClass = *(stream->isa);      /* Copy pointers to procedures */
1.10      timbl     979:     if (rep_in == WWW_BINARY || HTOutputSource
1.26      luotonen  980:        || (request->content_encoding &&
                    981:            request->content_encoding != HTAtom_for("8bit") &&
                    982:            request->content_encoding != HTAtom_for("7bit"))
1.10      timbl     983:         || strstr(HTAtom_name(rep_in), "image/")
                    984:        || strstr(HTAtom_name(rep_in), "video/")) { /* @@@@@@ */
1.2       timbl     985:         HTCopy(file_number, stream);
                    986:     } else {   /* ascii text with CRLFs :-( */
                    987:         HTCopyNoCR(file_number, stream);
                    988:     }
1.7       secret    989:     (*targetClass.free)(stream);
                    990:     
                    991:     return HT_LOADED;
                    992: }
                    993: 
                    994: 
                    995: 
                    996: /*     Parse a file given format and file pointer
                    997: **
                    998: **   This routine is responsible for creating and PRESENTING any
                    999: **   graphic (or other) objects described by the file.
                   1000: **
                   1001: **   The file number given is assumed to be a TELNET stream ie containing
1.10      timbl    1002: **   CRLF at the end of lines which need to be stripped to \n for unix
1.7       secret   1003: **   when the format is textual.
                   1004: **
                   1005: */
1.12      timbl    1006: PUBLIC int HTParseFile ARGS3(
1.10      timbl    1007:        HTFormat,               rep_in,
1.7       secret   1008:        FILE *,                 fp,
1.12      timbl    1009:        HTRequest *,            request)
1.7       secret   1010: {
                   1011:     HTStream * stream;
                   1012:     HTStreamClass targetClass;    
                   1013: 
1.12      timbl    1014:     stream = HTStreamStack(rep_in, request);
1.7       secret   1015:     
                   1016:     if (!stream) {
                   1017:         char buffer[1024];     /* @@@@@@@@ */
                   1018:        sprintf(buffer, "Sorry, can't convert from %s to %s.",
1.12      timbl    1019:                HTAtom_name(rep_in), HTAtom_name(request->output_format));
1.7       secret   1020:        if (TRACE) fprintf(stderr, "HTFormat(in HTParseFile): %s\n", buffer);
1.16      luotonen 1021:         return HTLoadError(request, 501, buffer);
1.7       secret   1022:     }
                   1023:     
1.9       timbl    1024: /*     Push the data down the stream
1.7       secret   1025: **
                   1026: **
                   1027: **   @@  Bug:  This decision ought to be made based on "encoding"
1.10      timbl    1028: **   rather than on content-type.  @@@  When we handle encoding.
1.7       secret   1029: **   The current method smells anyway.
                   1030: */
                   1031:     targetClass = *(stream->isa);      /* Copy pointers to procedures */
                   1032:     HTFileCopy(fp, stream);
1.2       timbl    1033:     (*targetClass.free)(stream);
1.1       timbl    1034:     
1.2       timbl    1035:     return HT_LOADED;
1.1       timbl    1036: }
1.2       timbl    1037: 
1.10      timbl    1038: 
                   1039: /*     Converter stream: Network Telnet to internal character text
                   1040: **     -----------------------------------------------------------
                   1041: **
                   1042: **     The input is assumed to be in ASCII, with lines delimited
                   1043: **     by (13,10) pairs, These pairs are converted into (CR,LF)
                   1044: **     pairs in the local representation.  The (CR,LF) sequence
                   1045: **     when found is changed to a '\n' character, the internal
                   1046: **     C representation of a new line.
                   1047: */
                   1048: 
                   1049: 
1.11      timbl    1050: PRIVATE void NetToText_put_character ARGS2(HTStream *, me, char, net_char)
1.10      timbl    1051: {
                   1052:     char c = FROMASCII(net_char);
                   1053:     if (me->had_cr) {
                   1054:         if (c==LF) {
                   1055:            me->sink->isa->put_character(me->sink, '\n');       /* Newline */
                   1056:            me->had_cr = NO;
                   1057:            return;
                   1058:         } else {
                   1059:            me->sink->isa->put_character(me->sink, CR); /* leftover */
                   1060:        }
                   1061:     }
                   1062:     me->had_cr = (c==CR);
                   1063:     if (!me->had_cr)
                   1064:        me->sink->isa->put_character(me->sink, c);              /* normal */
                   1065: }
                   1066: 
1.11      timbl    1067: PRIVATE void NetToText_put_string ARGS2(HTStream *, me, CONST char *, s)
1.10      timbl    1068: {
                   1069:     CONST char * p;
                   1070:     for(p=s; *p; p++) NetToText_put_character(me, *p);
                   1071: }
                   1072: 
1.11      timbl    1073: PRIVATE void NetToText_put_block ARGS3(HTStream *, me, CONST char*, s, int, l)
1.10      timbl    1074: {
                   1075:     CONST char * p;
                   1076:     for(p=s; p<(s+l); p++) NetToText_put_character(me, *p);
                   1077: }
                   1078: 
                   1079: PRIVATE void NetToText_free ARGS1(HTStream *, me)
                   1080: {
                   1081:     me->sink->isa->free(me->sink);             /* Close rest of pipe */
                   1082:     free(me);
                   1083: }
                   1084: 
                   1085: PRIVATE void NetToText_abort ARGS2(HTStream *, me, HTError, e)
                   1086: {
                   1087:     me->sink->isa->abort(me->sink,e);          /* Abort rest of pipe */
                   1088:     free(me);
                   1089: }
                   1090: 
                   1091: /*     The class structure
                   1092: */
                   1093: PRIVATE HTStreamClass NetToTextClass = {
                   1094:     "NetToText",
                   1095:     NetToText_free,
                   1096:     NetToText_abort,
                   1097:     NetToText_put_character,
                   1098:     NetToText_put_string,
                   1099:     NetToText_put_block
                   1100: };
                   1101: 
                   1102: /*     The creation method
                   1103: */
                   1104: PUBLIC HTStream * HTNetToText ARGS1(HTStream *, sink)
                   1105: {
                   1106:     HTStream* me = (HTStream*)malloc(sizeof(*me));
                   1107:     if (me == NULL) outofmem(__FILE__, "NetToText");
                   1108:     me->isa = &NetToTextClass;
                   1109:     
                   1110:     me->had_cr = NO;
                   1111:     me->sink = sink;
                   1112:     return me;
                   1113: }
1.2       timbl    1114: 
                   1115: 

Webmaster