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

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

Webmaster