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

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

Webmaster