Annotation of libwww/Library/src/HTReader.c, revision 2.8.2.1

2.1       frystyk     1: /*                                                                  HTReader.c
                      2: **     READ STREAM FROM THE NETWORK USING TCP
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.8.2.1 ! eric        6: **     @(#) $Id: HTReader.c,v 2.8 1996/08/21 14:10:31 frystyk Exp $
2.1       frystyk     7: **
                      8: ** HISTORY:
                      9: **     6 June 95  HFN  Written
                     10: */
                     11: 
                     12: /* Library Include files */
                     13: #include "sysdep.h"
                     14: #include "WWWUtil.h"
2.5       frystyk    15: #include "WWWCore.h"
2.8.2.1 ! eric       16: #include "HTHost.h"
2.1       frystyk    17: #include "HTNetMan.h"
                     18: #include "HTReader.h"                                   /* Implemented here */
                     19: 
                     20: struct _HTStream {
                     21:     const HTStreamClass *      isa;
                     22:     /* ... */
                     23: };
                     24: 
                     25: struct _HTInputStream {
                     26:     const HTInputStreamClass * isa;
                     27:     HTChannel *                        ch;
2.8.2.1 ! eric       28:     HTHost *                   host;
2.1       frystyk    29:     HTStream *                 target;          /* Target for incoming data */
                     30:     char *                     write;                  /* Last byte written */
                     31:     char *                     read;                      /* Last byte read */
                     32:     char                       data [INPUT_BUFFER_SIZE];          /* buffer */
2.8.2.1 ! eric       33:     int                                b_read;
2.1       frystyk    34: };
                     35: 
                     36: /* ------------------------------------------------------------------------- */
                     37: 
                     38: PRIVATE int HTReader_flush (HTInputStream * me)
                     39: {
2.8.2.1 ! eric       40:     HTNet * net = HTHost_getReadNet(me->host);
        !            41:     return net && net->readStream ? (*net->readStream->isa->flush)(me->target) : HT_OK;
2.1       frystyk    42: }
                     43: 
                     44: PRIVATE int HTReader_free (HTInputStream * me)
                     45: {
2.8.2.1 ! eric       46:     HTNet * net = HTHost_getReadNet(me->host);
        !            47:     if (net && net->readStream) {
        !            48:        int status = (*net->readStream->isa->_free)(me->target);
        !            49:        /*      if (status != HT_WOULD_BLOCK) me->target = NULL; */
2.1       frystyk    50:        return status;
                     51:     }
                     52:     return HT_OK;
                     53: }
                     54: 
                     55: PRIVATE int HTReader_abort (HTInputStream * me, HTList * e)
                     56: {
2.8.2.1 ! eric       57:     HTNet * net = HTHost_getReadNet(me->host);
        !            58:     if (net && net->readStream) {
        !            59:        (*net->readStream->isa->abort)(me->target, NULL);
        !            60:        /*      me->target = NULL; */
2.1       frystyk    61:     }
                     62:     return HT_ERROR;
                     63: }
                     64: 
                     65: /*     Push data from a socket down a stream
                     66: **     -------------------------------------
                     67: **
                     68: **   This routine is responsible for creating and PRESENTING any
                     69: **   graphic (or other) objects described by the file. As this function
                     70: **   max reads a chunk of data on size INPUT_BUFFER_SIZE, it can be used
                     71: **   with both blocking or non-blocking sockets. It will always return to
                     72: **   the event loop, however if we are using blocking I/O then we get a full
                     73: **   buffer read, otherwise we get what's available.
                     74: **
                     75: ** Returns      HT_LOADED      if finished reading
                     76: **             HT_OK           if OK, but more to read
                     77: **             HT_ERROR        if error,
                     78: **                     HT_WOULD_BLOCK  if read or write would block
                     79: **             HT_PAUSE        if stream is paused
                     80: */
                     81: PRIVATE int HTReader_read (HTInputStream * me)
                     82: {
2.8.2.1 ! eric       83:     HTHost * host = me->host;
        !            84:     SOCKET soc = HTChannel_socket(HTHost_channel(host));
2.1       frystyk    85:     int status;
2.8.2.1 ! eric       86:     HTNet * net = HTHost_getReadNet(host);
2.1       frystyk    87: 
2.8.2.1 ! eric       88:     me->b_read = me->read - me->data;
2.1       frystyk    89:     /* Read from socket if we got rid of all the data previously read */
                     90:     do {
2.8.2.1 ! eric       91:        /* don't read if we have to push unwritten data from last call */
2.1       frystyk    92:        if (me->write >= me->read) {
2.8.2.1 ! eric       93:            if ((me->b_read = NETREAD(soc, me->data, INPUT_BUFFER_SIZE)) < 0) {
2.1       frystyk    94: #ifdef EAGAIN
                     95:                if (socerrno==EAGAIN || socerrno==EWOULDBLOCK)      /* POSIX */
                     96: #else
                     97:                if (socerrno==EWOULDBLOCK)                            /* BSD */
                     98: #endif 
                     99:                {
                    100:                    if (PROT_TRACE)
                    101:                        HTTrace("Read Socket. WOULD BLOCK fd %d\n",soc);
2.8.2.1 ! eric      102:                    HTHost_register(host, net, HTEvent_READ);
2.1       frystyk   103:                    return HT_WOULD_BLOCK;
                    104: #ifdef __svr4__
                    105:     /* 
                    106:     ** In Solaris envirnoment, SIGPOLL is used to signal end of buffer for
                    107:     ** /dev/audio.  If your process is also doing a socket read, it will cause
                    108:     ** an EINTR error.  This error will cause the www library request to 
                    109:     ** terminate prematurly.
                    110:     */
                    111:                 } else if (socerrno == EINTR) {
                    112:                     continue;
                    113: #endif /* __svr4__ */
                    114:                } else {                             /* We have a real error */
                    115: 
                    116:                    /* HERE WE SHOULD RETURN target abort */
                    117: 
                    118:                    HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
                    119:                                             NO, "NETREAD");
                    120:                    return HT_ERROR;
                    121:                }
2.8       frystyk   122: #ifdef ECONNRESET
2.8.2.1 ! eric      123:            } else if (!me->b_read || socerrno==ECONNRESET) {
2.8       frystyk   124: #else
2.8.2.1 ! eric      125:            } else if (!me->b_read) {
2.8       frystyk   126: #endif
2.1       frystyk   127:                HTAlertCallback *cbf = HTAlert_find(HT_PROG_DONE);
                    128:                if (PROT_TRACE)
2.3       frystyk   129:                    HTTrace("Read Socket. FIN received on socket %d\n", soc);
2.1       frystyk   130:                if (cbf) (*cbf)(net->request, HT_PROG_DONE,
                    131:                                HT_MSG_NULL, NULL, NULL, NULL);
2.8.2.1 ! eric      132:                HTHost_unregister(host, net, HTEvent_READ);
2.1       frystyk   133:                return HT_CLOSED;
                    134:            }
                    135: 
                    136:            /* Remember how much we have read from the input socket */
                    137:            me->write = me->data;
2.8.2.1 ! eric      138:            me->read = me->data + me->b_read;
2.1       frystyk   139: 
                    140: #ifdef NOT_ASCII
                    141:            {
                    142:                char *p = me->data;
                    143:                while (p < me->read) {
                    144:                    *p = FROMASCII(*p);
                    145:                    p++;
                    146:                }
                    147:            }
                    148: #endif /* NOT_ASCII */
                    149: 
                    150:            if (PROT_TRACE) 
                    151:                HTTrace("Read Socket. %d bytes read from socket %d\n",
2.8.2.1 ! eric      152:                        me->b_read, soc);
2.1       frystyk   153:            {
                    154:                HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
2.8.2.1 ! eric      155:                net->bytesRead += me->b_read;
2.1       frystyk   156:                if (cbf) (*cbf)(net->request, HT_PROG_READ,
                    157:                                HT_MSG_NULL, NULL, NULL, NULL);
                    158:            }
                    159:        }
                    160: 
                    161:        /* Now push the data down the stream */
2.8.2.1 ! eric      162:        if ((status = (*net->readStream->isa->put_block)
        !           163:             (net->readStream, me->data, me->b_read)) != HT_OK) {
2.1       frystyk   164:            if (status == HT_WOULD_BLOCK) {
                    165:                if (PROT_TRACE) HTTrace("Read Socket. Target WOULD BLOCK\n");
2.8.2.1 ! eric      166:                HTHost_unregister(host, net, HTEvent_READ);
2.1       frystyk   167:                return HT_WOULD_BLOCK;
                    168:            } else if (status == HT_PAUSE) {
                    169:                if (PROT_TRACE) HTTrace("Read Socket. Target PAUSED\n");
2.8.2.1 ! eric      170:                HTHost_unregister(host, net, HTEvent_READ);
2.1       frystyk   171:                return HT_PAUSE;
2.8.2.1 ! eric      172:            /* CONTINUE code or stream code means data was consumed */
        !           173:            } else if (status == HT_CONTINUE || status > 0) {
        !           174:                if (status == HT_CONTINUE) {
        !           175:                    if (PROT_TRACE) HTTrace("Read Socket. CONTINUE\n");
        !           176:                } else
        !           177:                    if (PROT_TRACE)
        !           178:                        HTTrace("Read Socket. Target returns %d\n", status);
        !           179: /*             me->write = me->read; */
2.1       frystyk   180:                return status;
                    181:            } else {                                 /* We have a real error */
                    182:                if (PROT_TRACE) HTTrace("Read Socket. Target ERROR\n");
                    183:                return status;
                    184:            }
                    185:        }
2.8.2.1 ! eric      186:        me->write = me->read;
        !           187:        HTHost_setRemainingRead(host, 0);
2.1       frystyk   188:     } while (net->preemptive);
2.8.2.1 ! eric      189:     HTHost_register(host, net, HTEvent_READ);
2.1       frystyk   190:     return HT_WOULD_BLOCK;
                    191: }
                    192: 
                    193: /*
                    194: **     The difference between the close and the free method is that we don't
                    195: **     close the connection in the free method - we only call the free method
                    196: **     of the target stream. That way, we can keep the input stream as long 
                    197: **     as the channel itself.
                    198: */
                    199: PRIVATE int HTReader_close (HTInputStream * me)
                    200: {
                    201:     int status = HT_OK;
2.8.2.1 ! eric      202:     HTNet * net = HTHost_getReadNet(me->host);
        !           203:     if (net && net->readStream) {
        !           204:        if ((status = (*net->readStream->isa->_free)(me->target))==HT_WOULD_BLOCK)
2.1       frystyk   205:            return HT_WOULD_BLOCK;
                    206:     }
                    207:     if (PROT_TRACE) HTTrace("Socket read. FREEING....\n");
                    208:     HT_FREE(me);
                    209:     return status;
                    210: }
                    211: 
2.8.2.1 ! eric      212: PUBLIC int HTReader_consumed (HTInputStream * me, size_t bytes)
        !           213: {
        !           214:     if (PROT_TRACE) HTTrace("ANSI read... consumed %d bytes\n", bytes);
        !           215:     me->write += bytes;
        !           216:     me->b_read -= bytes;
        !           217:     HTHost_setRemainingRead(me->host, me->b_read);
        !           218:     return HT_OK;
        !           219: }
        !           220: 
2.1       frystyk   221: PRIVATE const HTInputStreamClass HTReader =
                    222: {
                    223:     "SocketReader",
                    224:     HTReader_flush,
                    225:     HTReader_free,
                    226:     HTReader_abort,
                    227:     HTReader_read,
2.8.2.1 ! eric      228:     HTReader_close,
        !           229:     HTReader_consumed
2.1       frystyk   230: }; 
                    231: 
                    232: /*
                    233: **     Create a new input read stream. Before we actually create it we check
                    234: **     to see whether we already have an input stream for this channel and if
                    235: **     so we just return that. This means that we can reuse input streams 
                    236: **     in persistent connections, for example.
                    237: */
2.8.2.1 ! eric      238: PUBLIC HTInputStream * HTReader_new (HTHost * host, HTChannel * ch,
        !           239:                                     void * param, int mode)
2.1       frystyk   240: {
2.8.2.1 ! eric      241:     if (host && ch) {
2.1       frystyk   242:        HTInputStream * me = HTChannel_input(ch);
                    243:        if (me == NULL) {
                    244:            if ((me=(HTInputStream *) HT_CALLOC(1, sizeof(HTInputStream))) == NULL)
                    245:            HT_OUTOFMEM("HTReader_new");
                    246:            me->isa = &HTReader;
                    247:            me->ch = ch;
                    248:        }
2.8.2.1 ! eric      249:        me->host = host;
2.1       frystyk   250:        return me;
                    251:     }
                    252:     return NULL;
                    253: }

Webmaster