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

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

Webmaster