Annotation of libwww/Library/src/HTSocket.c, revision 2.3

2.1       frystyk     1: /*                                                                  HTSocket.c
                      2: **     MANAGES READ AND WRITE TO AND FROM THE NETWORK
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: **
                      8: ** HISTORY:
                      9: **     6 June 95  HFN  Spawned off from HTFormat
                     10: */
                     11: 
                     12: /* Library Include files */
                     13: #include "tcp.h"
                     14: #include "HTUtils.h"
                     15: #include "HTString.h"
2.2       frystyk    16: #include "HTAccess.h"
2.1       frystyk    17: #include "HTTCP.h"
                     18: #include "HTStream.h"
                     19: #include "HTFormat.h"
                     20: #include "HTThread.h"
                     21: #include "HTError.h"
                     22: #include "HTSocket.h"                                   /* Implemented here */
                     23: 
2.2       frystyk    24: struct _HTInputSocket {
                     25:     char       input_buffer[INPUT_BUFFER_SIZE];
                     26:     char *     input_pointer;
                     27:     char *     input_limit;
                     28:     SOCKFD     input_file_number;
                     29: };
                     30: 
2.1       frystyk    31: struct _HTStream {
                     32:     CONST HTStreamClass *      isa;
                     33: };
                     34: 
                     35: /* ------------------------------------------------------------------------- */
                     36: /*                             SOCKET INPUT BUFFERING                       */
                     37: /* ------------------------------------------------------------------------- */
                     38: /*                     
                     39: **     This code is used because one cannot in general open a
                     40: **     file descriptor for a socket.
                     41: **
                     42: **     The input file is read using the macro which can read from
                     43: **     a socket or a file, but this should not be used for files
                     44: **     as fopen() etc is more portable of course.
                     45: **
                     46: **     The input buffer size, if large will give greater efficiency and
                     47: **     release the server faster, and if small will save space on PCs etc.
                     48: */
                     49: 
                     50: 
                     51: /*     Set up the buffering
                     52: **
                     53: **     These routines are public because they are in fact needed by
                     54: **     many parsers, and on PCs and Macs we should not duplicate
                     55: **     the static buffer area.
                     56: */
                     57: PUBLIC HTInputSocket * HTInputSocket_new ARGS1 (SOCKFD, file_number)
                     58: {
                     59:     HTInputSocket *isoc = (HTInputSocket *)calloc(1, sizeof(*isoc));
                     60:     if (!isoc) outofmem(__FILE__, "HTInputSocket_new");
                     61:     isoc->input_file_number = file_number;
                     62:     isoc->input_pointer = isoc->input_limit = isoc->input_buffer;
                     63:     return isoc;
                     64: }
                     65: 
                     66: /* This should return HT_INTERRUPTED if interrupted BUT the connection
                     67:    MUST not be closed */ 
                     68: PUBLIC int HTInputSocket_getCharacter ARGS1(HTInputSocket*, isoc)
                     69: {
                     70:     int ch;
                     71:     do {
                     72:        if (isoc->input_pointer >= isoc->input_limit) {
                     73:            int status = NETREAD(isoc->input_file_number,
                     74:                                 isoc->input_buffer, INPUT_BUFFER_SIZE);
                     75:            if (status <= 0) {
                     76:                if (status == 0)
                     77:                    return EOF;
                     78:                if (status == HT_INTERRUPTED) {
                     79:                    if (TRACE)
                     80:                        fprintf(TDEST, "Get Char.... Interrupted in HTInputSocket_getCharacter\n");
                     81:                    return HT_INTERRUPTED;
                     82:                }
                     83:                if (PROT_TRACE)
                     84:                    fprintf(TDEST, "Read Socket. READ ERROR %d\n", socerrno);
                     85:                return EOF;     /* -1 is returned by UCX at end of HTTP link */
                     86:            }
                     87:            isoc->input_pointer = isoc->input_buffer;
                     88:            isoc->input_limit = isoc->input_buffer + status;
                     89:        }
                     90:        ch = (unsigned char) *isoc->input_pointer++;
                     91:     } while (ch == 13);                             /* Ignore ASCII carriage return */
                     92:     
                     93:     return FROMASCII(ch);
                     94: }
                     95: 
                     96: PUBLIC void HTInputSocket_free ARGS1(HTInputSocket *, me)
                     97: {
                     98:     if (me) free(me);
                     99: }
                    100: 
                    101: 
                    102: PUBLIC char * HTInputSocket_getBlock ARGS2(HTInputSocket*,     isoc,
                    103:                                           int *,               len)
                    104: {
                    105:     if (isoc->input_pointer >= isoc->input_limit) {
                    106:        int status = NETREAD(isoc->input_file_number,
                    107:                             isoc->input_buffer,
                    108:                             ((*len < INPUT_BUFFER_SIZE) ?
                    109:                              *len : INPUT_BUFFER_SIZE));
                    110:        if (status <= 0) {
                    111:            isoc->input_limit = isoc->input_buffer;
                    112:            if (status < 0) {
                    113:                if (PROT_TRACE)
                    114:                    fprintf(TDEST, "Read Socket. READ ERROR %d\n", socerrno);
                    115:            }
                    116:            *len = 0;
                    117:            return NULL;
                    118:        }
                    119:        else {
                    120:            *len = status;
                    121:            return isoc->input_buffer;
                    122:        }
                    123:     }
                    124:     else {
                    125:        char * ret = isoc->input_pointer;
                    126:        *len = isoc->input_limit - isoc->input_pointer;
                    127:        isoc->input_pointer = isoc->input_limit;
                    128:        return ret;
                    129:     }
                    130: }
                    131: 
                    132: 
                    133: PRIVATE int fill_in_buffer ARGS1(HTInputSocket *, isoc)
                    134: {
                    135:     if (isoc) {
                    136:        int status;
                    137: 
                    138:        isoc->input_pointer = isoc->input_buffer;
                    139:        status = NETREAD(isoc->input_file_number,
                    140:                         isoc->input_buffer,
                    141:                         INPUT_BUFFER_SIZE);
                    142:        if (status <= 0) {
                    143:            isoc->input_limit = isoc->input_buffer;
                    144:            if (status < 0) {
                    145:                if (PROT_TRACE)
                    146:                    fprintf(TDEST, "Read Socket. READ ERROR %d\n", socerrno);
                    147:            }
                    148:        }
                    149:        else 
                    150:            isoc->input_limit = isoc->input_buffer + status;
                    151:        return status;
                    152:     }
                    153:     return -1;
                    154: }
                    155: 
                    156: 
                    157: PRIVATE void ascii_cat ARGS3(char **,  linep,
                    158:                             char *,    start,
                    159:                             char *,    end)
                    160: {
                    161:     if (linep && start && end && start <= end) {
                    162:        char *ptr;
                    163: 
                    164:        if (*linep) {
                    165:            int len = strlen(*linep);
                    166:            *linep = (char*)realloc(*linep, len + end-start + 1);
                    167:            ptr = *linep + len;
                    168:        }
                    169:        else {
                    170:            ptr = *linep = (char*)malloc(end-start + 1);
                    171:        }
                    172: 
                    173:        while (start < end) {
                    174:            *ptr = FROMASCII(*start);
                    175:            ptr++;
                    176:            start++;
                    177:        }
                    178:        *ptr = 0;
                    179:     }
                    180: }
                    181: 
                    182: 
                    183: PRIVATE char * get_some_line ARGS2(HTInputSocket *,    isoc,
                    184:                                   BOOL,                unfold)
                    185: {
                    186:     if (!isoc)
                    187:        return NULL;
                    188:     else {
                    189:        BOOL check_unfold = NO;
                    190:        int prev_cr = 0;
                    191:        char *start = isoc->input_pointer;
                    192:        char *cur = isoc->input_pointer;
                    193:        char * line = NULL;
                    194: 
                    195:        for(;;) {
                    196:            /*
                    197:            ** Get more if needed to complete line
                    198:            */
                    199:            if (cur >= isoc->input_limit) { /* Need more data */
                    200:                ascii_cat(&line, start, cur);
                    201:                if (fill_in_buffer(isoc) <= 0)
                    202:                    return line;
                    203:                start = cur = isoc->input_pointer;
                    204:            } /* if need more data */
                    205: 
                    206:            /*
                    207:            ** Find a line feed if there is one
                    208:            */
                    209:            for(; cur < isoc->input_limit; cur++) {
                    210:                char c = FROMASCII(*cur);
                    211:                if (!c) {
                    212:                    if (line) free(line);       /* Leak fixed AL 6 Feb 94 */
                    213:                    return NULL;        /* Panic! read a 0! */
                    214:                }
                    215:                if (check_unfold  &&  c != ' '  &&  c != '\t') {
                    216:                    return line;  /* Note: didn't update isoc->input_pointer */
                    217:                }
                    218:                else {
                    219:                    check_unfold = NO;
                    220:                }
                    221: 
                    222:                if (c=='\r') {
                    223:                    prev_cr = 1;
                    224:                }
                    225:                else {
                    226:                    if (c=='\n') {              /* Found a line feed */
                    227:                        ascii_cat(&line, start, cur-prev_cr);
                    228:                        start = isoc->input_pointer = cur+1;
                    229: 
                    230:                        if (line && (int) strlen(line) > 0 && unfold) {
                    231:                            check_unfold = YES;
                    232:                        }
                    233:                        else {
                    234:                            return line;
                    235:                        }
                    236:                    } /* if NL */
                    237:                    /* else just a regular character */
                    238:                    prev_cr = 0;
                    239:                } /* if not CR */
                    240:            } /* while characters in buffer remain */
                    241:        } /* until line read or end-of-file */
                    242:     } /* valid parameters to function */
                    243: }
                    244: 
                    245: /* The returned string must be freed by the caller */
                    246: PUBLIC char * HTInputSocket_getLine ARGS1(HTInputSocket *, isoc)
                    247: {
                    248:     return get_some_line(isoc, NO);
                    249: }
                    250: 
                    251: /* The returned string must be freed by the caller */
                    252: PUBLIC char * HTInputSocket_getUnfoldedLine ARGS1(HTInputSocket *, isoc)
                    253: {
                    254:     return get_some_line(isoc, YES);
                    255: }
                    256: 
                    257: 
                    258: /*     Push data from a socket down a stream
                    259: **     -------------------------------------
                    260: **
                    261: **   This routine is responsible for creating and PRESENTING any
                    262: **   graphic (or other) objects described by the file.
                    263: **
                    264: **   The file number given is assumed to be a TELNET stream ie containing
                    265: **   CRLF at the end of lines which need to be stripped to LF for unix
                    266: **   when the format is textual.
                    267: **
                    268: **   RETURNS the number of bytes transferred.
                    269: **
                    270: */
                    271: PUBLIC int HTCopy ARGS2(
                    272:        SOCKFD,                 file_number,
                    273:        HTStream*,              sink)
                    274: {
                    275:     HTStreamClass targetClass;    
                    276:     HTInputSocket * isoc;
                    277:     int cnt = 0;
                    278: 
                    279: /*     Push the data down the stream
                    280: **
                    281: */
                    282:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
                    283:     isoc = HTInputSocket_new(file_number);
                    284:     
                    285:     /* Push binary from socket down sink
                    286:     **
                    287:     **         This operation could be put into a main event loop
                    288:     */
                    289:     for(;;) {
                    290:        int status = NETREAD(
                    291:                file_number, isoc->input_buffer, INPUT_BUFFER_SIZE);
                    292:        if (status <= 0) {
                    293:            if (status == 0) break;
                    294:            if (TRACE) fprintf(TDEST,
                    295:                "Socket Copy. Read error, read returns %d with errno=%d\n",
                    296:                status, socerrno);
                    297:            break;
                    298:        }
                    299: 
                    300: #ifdef NOT_ASCII
                    301:        {
                    302:            char * p;
                    303:            for(p = isoc->input_buffer; p < isoc->input_buffer+status; p++) {
                    304:                *p = FROMASCII(*p);
                    305:            }
                    306:        }
                    307: #endif
                    308: 
                    309:        (*targetClass.put_block)(sink, isoc->input_buffer, status);
                    310:        cnt += status;
                    311:     } /* next bufferload */
                    312: 
                    313:     HTInputSocket_free(isoc);
                    314: 
                    315:     return cnt;
                    316: }
                    317: 
                    318: 
                    319: 
                    320: /*     Push data from a file pointer down a stream
                    321: **     -------------------------------------
                    322: **
                    323: **   This routine is responsible for creating and PRESENTING any
                    324: **   graphic (or other) objects described by the file.
                    325: **
                    326: **
                    327: */
                    328: PUBLIC void HTFileCopy ARGS2(
                    329:        FILE *,                 fp,
                    330:        HTStream*,              sink)
                    331: {
                    332:     HTStreamClass targetClass;    
                    333:     char input_buffer[INPUT_BUFFER_SIZE];
                    334:     
                    335: /*     Push the data down the stream
                    336: **
                    337: */
                    338:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
                    339:     
                    340:     /* Push binary from socket down sink
                    341:     */
                    342:     for(;;) {
                    343:        int status = fread(
                    344:               input_buffer, 1, INPUT_BUFFER_SIZE, fp);
                    345:        if (status == 0) { /* EOF or error */
                    346:            if (ferror(fp) == 0) break;
                    347:            if (TRACE) fprintf(TDEST,
                    348:                "File Copy... Read error, read returns %d\n", ferror(fp));
                    349:            break;
                    350:        }
                    351:        (*targetClass.put_block)(sink, input_buffer, status);
                    352:     } /* next bufferload */    
                    353: }
                    354: 
                    355: 
                    356: 
                    357: 
                    358: /*     Push data from a socket down a stream STRIPPING CR
                    359: **     --------------------------------------------------
                    360: **
                    361: **   This routine is responsible for creating and PRESENTING any
                    362: **   graphic (or other) objects described by the socket.
                    363: **
                    364: **   The file number given is assumed to be a TELNET stream ie containing
                    365: **   CRLF at the end of lines which need to be stripped to LF for unix
                    366: **   when the format is textual.
                    367: **     
                    368: **     Character handling is now of type int, Henrik, May 09-94
                    369: */
                    370: PUBLIC void HTCopyNoCR ARGS2(
                    371:        SOCKFD,                 file_number,
                    372:        HTStream*,              sink)
                    373: {
                    374:     HTStreamClass targetClass;
                    375:     HTInputSocket * isoc;   
                    376:     int ch;
                    377:     
                    378: /*     Push the data, ignoring CRLF, down the stream
                    379: **
                    380: */
                    381:     targetClass = *(sink->isa);        /* Copy pointers to procedures */
                    382: 
                    383: /*     Push text from telnet socket down sink
                    384: **
                    385: **     @@@@@ To push strings could be faster? (especially is we
                    386: **     cheat and don't ignore CR! :-}
                    387: */  
                    388:     isoc = HTInputSocket_new(file_number);
                    389:     while ((ch = HTInputSocket_getCharacter(isoc)) >= 0)
                    390:        (*targetClass.put_character)(sink, ch);
                    391:     HTInputSocket_free(isoc);
                    392: }
                    393: 
                    394: 
                    395: /*     Parse a socket given format and file number
                    396: **
                    397: **   This routine is responsible for creating and PRESENTING any
                    398: **   graphic (or other) objects described by the file.
                    399: **
                    400: **   The file number given is assumed to be a TELNET stream ie containing
                    401: **   CRLF at the end of lines which need to be stripped to LF for unix
                    402: **   when the format is textual.
                    403: **
                    404: **     Returns <0 on error, HT_LOADED on success.
                    405: */
                    406: 
                    407: /* The parameter to this function and HTParsefile should be HTRequest */
                    408: 
                    409: PUBLIC int HTParseSocket ARGS3(
                    410:        HTFormat,               rep_in,
                    411:        SOCKFD,                 file_number,
                    412:        HTRequest *,            request)
                    413: {
                    414:     HTStream * stream;
                    415:     HTStreamClass targetClass;    
                    416: 
                    417:     if (request->error_stack) {
                    418:        if (TRACE) fprintf(TDEST, "ParseSocket. Called whith non-empty error stack, so I return right away!\n");
                    419:        return -1;
                    420:     }
                    421: 
                    422:     /* Set up stream stack */
                    423:     if ((stream = HTStreamStack(rep_in, request->output_format,
                    424:                                request->output_stream,
                    425:                                request, YES)) == NULL)
                    426:        return -1;
                    427:     
                    428: /*     Push the data, ignoring CRLF if necessary, down the stream
                    429: **
                    430: **
                    431: **   @@  Bug:  This decision ought to be made based on "encoding"
                    432: **   rather than on format.  @@@  When we handle encoding.
                    433: **   The current method smells anyway.
                    434: */
                    435:     targetClass = *(stream->isa);      /* Copy pointers to procedures */
                    436:     if (rep_in == WWW_BINARY || rep_in == WWW_UNKNOWN
                    437:        || (HTAnchor_encoding(request->anchor) != HTAtom_for("8bit") &&
                    438:            HTAnchor_encoding(request->anchor) != HTAtom_for("7bit"))
                    439:         || strstr(HTAtom_name(rep_in), "image/")
                    440:        || strstr(HTAtom_name(rep_in), "video/")) { /* @@@@@@ */
                    441:        HTCopy(file_number, stream);
                    442:     } else
                    443:         HTCopyNoCR(file_number, stream);
                    444:     (*targetClass._free)(stream);
                    445:     
                    446:     return HT_LOADED;
                    447: }
                    448: 
                    449: 
                    450: 
                    451: /*     Parse a file given format and file pointer
                    452: **
                    453: **   This routine is responsible for creating and PRESENTING any
                    454: **   graphic (or other) objects described by the file.
                    455: **
                    456: **   The file number given is assumed to be a TELNET stream ie containing
                    457: **   CRLF at the end of lines which need to be stripped to \n for unix
                    458: **   when the format is textual.
                    459: **
                    460: */
2.2       frystyk   461: PRIVATE int HTParseFile ARGS3(
2.1       frystyk   462:        HTFormat,               rep_in,
                    463:        FILE *,                 fp,
                    464:        HTRequest *,            request)
                    465: {
                    466:     HTStream * stream;
                    467:     HTStreamClass targetClass;    
                    468: 
                    469:     if (request->error_stack) {
                    470:        if (TRACE) fprintf(TDEST, "ParseFile... Called whith non-empty error stack, so I return right away!\n");
                    471:        return -1;
                    472:     }
                    473: 
                    474:     /* Set up stream stack */
                    475:     if ((stream = HTStreamStack(rep_in, request->output_format,
                    476:                                request->output_stream, request, YES)) == NULL)
                    477:        return -1;
                    478:     
                    479: /*     Push the data down the stream
                    480: **
                    481: **
                    482: **   @@  Bug:  This decision ought to be made based on "encoding"
                    483: **   rather than on content-type.  @@@  When we handle encoding.
                    484: **   The current method smells anyway.
                    485: */
                    486:     targetClass = *(stream->isa);      /* Copy pointers to procedures */
                    487:     HTFileCopy(fp, stream);
                    488:     (*targetClass._free)(stream);
                    489:     
                    490:     return HT_LOADED;
                    491: }
                    492: 
                    493: 
                    494: /* ------------------------------------------------------------------------- */
                    495: /*                     MULTI THREADED IMPLEMENTATIONS                       */
                    496: /* ------------------------------------------------------------------------- */
                    497: 
                    498: /*     Push data from a socket down a stream
                    499: **     -------------------------------------
                    500: **
                    501: **   This routine is responsible for creating and PRESENTING any
                    502: **   graphic (or other) objects described by the file. As this function
                    503: **   max reads a chunk of data on size INPUT_BUFFER_SIZE, it can be used
                    504: **   with both blocking or non-blocking sockets. It will always return to
                    505: **   the event loop, however if we are using blocking I/O then we get a full
                    506: **   buffer read, otherwise we get what's available.
                    507: **
                    508: ** Returns      HT_LOADED      if finished reading
2.3     ! frystyk   509: **             HT_OK           if OK, but more to read
2.1       frystyk   510: **             HT_ERROR        if error,
                    511: **             HT_INTERRUPTED  if interrupted
                    512: **                     HT_WOULD_BLOCK  if read would block
                    513: */
                    514: PUBLIC int HTSocketRead ARGS2(HTRequest *, request, HTStream *, target)
                    515: {
                    516:     HTInputSocket *isoc = request->net_info->isoc;
                    517:     int b_read = isoc->input_limit-isoc->input_buffer;
                    518:     int status;
                    519:     if (!isoc || isoc->input_file_number==INVSOC) {
                    520:        if (PROT_TRACE) fprintf(TDEST, "Read Socket. Bad argument\n");
                    521:        return HT_ERROR;
                    522:     }
                    523: 
                    524:     if (HTThreadIntr(isoc->input_file_number))               /* Interrupted */
                    525:        return HT_INTERRUPTED;
2.3     ! frystyk   526: 
        !           527:     /* Read from socket if we got rid of all the data previously read */
        !           528:     if (isoc->input_pointer >= isoc->input_limit) {
        !           529:        if ((b_read = NETREAD(isoc->input_file_number, isoc->input_buffer,
        !           530:                              INPUT_BUFFER_SIZE)) < 0) {
2.1       frystyk   531: #ifdef EAGAIN
2.3     ! frystyk   532:            if (socerrno==EAGAIN || socerrno==EWOULDBLOCK)    /* POSIX, SVR4 */
2.1       frystyk   533: #else
2.3     ! frystyk   534:            if (socerrno==EWOULDBLOCK) /* BSD */
2.1       frystyk   535: #endif
2.3     ! frystyk   536:            {
        !           537:                if (PROT_TRACE)
        !           538:                    fprintf(TDEST, "Read Socket. WOULD BLOCK soc %d\n",
        !           539:                            isoc->input_file_number);
        !           540:                HTThreadState(isoc->input_file_number, THD_SET_READ);
        !           541:                return HT_WOULD_BLOCK;
        !           542:            } else {                                 /* We have a real error */
        !           543:                if (PROT_TRACE)
        !           544:                    fprintf(TDEST, "Read Socket. READ ERROR %d\n", socerrno);
        !           545:                return HT_ERROR;
2.1       frystyk   546:            }
2.3     ! frystyk   547:        } else if (!b_read) {
        !           548:            if (PROT_TRACE)
        !           549:                fprintf(TDEST, "Read Socket. Finished loading socket %d\n",
        !           550:                        isoc->input_file_number);
        !           551:            HTThreadState(isoc->input_file_number, THD_CLR_READ);
        !           552:            return HT_LOADED;
        !           553:        }
2.1       frystyk   554: 
2.3     ! frystyk   555:        /* Remember how much we have read from the input socket */
        !           556:        isoc->input_pointer = isoc->input_buffer;
        !           557:        isoc->input_limit = isoc->input_buffer + b_read;
2.1       frystyk   558: 
                    559: #ifdef NOT_ASCII
2.3     ! frystyk   560:        {
        !           561:            char *p = isoc->input_buffer;
        !           562:            while (p < isoc->input_limit) {
        !           563:                *p = FROMASCII(*p);
        !           564:                p++;
2.1       frystyk   565:            }
2.3     ! frystyk   566:        }
2.1       frystyk   567: #endif
2.3     ! frystyk   568:        if (PROT_TRACE)
        !           569:            fprintf(TDEST, "Read Socket. %d bytes read from socket %d\n",
        !           570:                    b_read, isoc->input_file_number);
        !           571:     }
        !           572:     
        !           573:     /* Now push the data down the stream */
        !           574:     if ((status = (*target->isa->put_block)(target, isoc->input_buffer,
        !           575:                                            b_read)) != HT_OK) {
        !           576:        if (status==HT_WOULD_BLOCK) {
        !           577:            if (PROT_TRACE)
        !           578:                fprintf(TDEST, "Read Socket. Stream WOULD BLOCK\n");
        !           579:            HTThreadState(isoc->input_file_number, THD_CLR_READ);
        !           580:            return HT_WOULD_BLOCK;
        !           581:        } else {                /* We have a real error */
2.1       frystyk   582:            if (PROT_TRACE)
2.3     ! frystyk   583:                fprintf(TDEST, "Read Socket. Stream ERROR\n");
        !           584:            return status;
2.1       frystyk   585:        }
                    586:     }
2.3     ! frystyk   587:     isoc->input_pointer = isoc->input_buffer + b_read;
        !           588:     HTThreadState(isoc->input_file_number, THD_SET_READ);
2.1       frystyk   589:     return HT_WOULD_BLOCK;
                    590: }
2.2       frystyk   591: 
                    592: 
                    593: 
                    594: /*     Push data from an ANSI file descriptor down a stream
                    595: **     ----------------------------------------------------
                    596: **
                    597: **   This routine is responsible for creating and PRESENTING any
                    598: **   graphic (or other) objects described by the file.
                    599: **
                    600: **   Bugs: When we can wait on a file then this should also check interrupts!
                    601: **
                    602: **   Returns    HT_LOADED      if finished reading
                    603: **             HT_ERROR        if error,
                    604: */
                    605: PUBLIC int HTFileRead ARGS3(FILE *, fp, HTRequest *, request,
                    606:                            HTStream *, target)
                    607: {
                    608:     HTInputSocket *isoc = request->net_info->isoc;
                    609:     int b_read;
                    610:     int status;
                    611:     if (!fp) {
                    612:        if (PROT_TRACE) fprintf(TDEST, "Read File... Bad argument\n");
                    613:        return HT_ERROR;
                    614:     }
                    615: 
                    616:     while(1) {
                    617:        if ((b_read = fread(isoc->input_buffer, 1, INPUT_BUFFER_SIZE, fp))==0){
                    618:            if (ferror(fp)) {
                    619:                if (PROT_TRACE)
                    620:                    fprintf(TDEST, "Read File... READ ERROR\n");
                    621:            } else
                    622:                return HT_LOADED;
                    623:        }
                    624:        isoc->input_pointer = isoc->input_buffer;
                    625:        isoc->input_limit = isoc->input_buffer + b_read;
                    626:        if (PROT_TRACE)
                    627:            fprintf(TDEST, "Read File... %d bytes read from file %p\n",
                    628:                    b_read, fp);
                    629: 
                    630:        /* Now push the data down the stream (we use blocking I/O) */
                    631:        if ((status = (*target->isa->put_block)(target, isoc->input_buffer,
                    632:                                                b_read)) != HT_OK) {
                    633:            if (PROT_TRACE)
                    634:                fprintf(TDEST, "Read File... Stream ERROR\n");
                    635:            return status;
                    636:        }
                    637:        isoc->input_pointer = isoc->input_buffer + b_read;
                    638:     }
                    639: }
                    640: 
                    641: 

Webmaster