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

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.29    ! frystyk     6: **     @(#) $Id: HTReader.c,v 2.28 1998/11/20 15:53:48 frystyk Exp $
2.1       frystyk     7: **
                      8: ** HISTORY:
                      9: **     6 June 95  HFN  Written
                     10: */
                     11: 
                     12: /* Library Include files */
2.23      frystyk    13: #include "wwwsys.h"
2.1       frystyk    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);
2.19      frystyk    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.20      frystyk    98: /* int DebugBufferSize = INPUT_BUFFER_SIZE; */
2.9       frystyk    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;
2.19      frystyk   107:     if (!net->readStream) {
2.29    ! frystyk   108:        HTTRACE(STREAM_TRACE, "Read Socket. No read stream for net object %p\n" _ net);
2.19      frystyk   109:         return HT_ERROR;
                    110:     }
                    111:         
2.9       frystyk   112:     /*    me->b_read = me->read - me->data; */
2.1       frystyk   113:     /* Read from socket if we got rid of all the data previously read */
                    114:     do {
2.9       frystyk   115:        /* don't read if we have to push unwritten data from last call */
2.1       frystyk   116:        if (me->write >= me->read) {
2.20      frystyk   117:            if ((me->b_read = NETREAD(soc, me->data, INPUT_BUFFER_SIZE)) < 0) {
2.1       frystyk   118: #ifdef EAGAIN
                    119:                if (socerrno==EAGAIN || socerrno==EWOULDBLOCK)      /* POSIX */
                    120: #else
                    121:                if (socerrno==EWOULDBLOCK)                            /* BSD */
                    122: #endif 
                    123:                {
2.29    ! frystyk   124:                    HTTRACE(STREAM_TRACE, "Read Socket. WOULD BLOCK fd %d\n" _ soc);
2.9       frystyk   125:                    HTHost_register(host, net, HTEvent_READ);
2.1       frystyk   126:                    return HT_WOULD_BLOCK;
                    127: #ifdef __svr4__
2.27      frystyk   128:                    /* 
                    129:                    ** In Solaris envirnoment, SIGPOLL is used to signal end of buffer for
                    130:                    ** /dev/audio.  If your process is also doing a socket read, it will cause
                    131:                    ** an EINTR error.  This error will cause the www library request to 
                    132:                    ** terminate prematurly.
                    133:                    */
2.1       frystyk   134:                 } else if (socerrno == EINTR) {
                    135:                     continue;
                    136: #endif /* __svr4__ */
2.9       frystyk   137: #ifdef EPIPE
                    138:                } else if (socerrno == EPIPE) {
2.29    ! frystyk   139:                    HTTRACE(STREAM_TRACE, "Read Socket. got EPIPE\n" _ soc);
2.9       frystyk   140:                    goto socketClosed;
                    141: #endif /* EPIPE */
2.27      frystyk   142: #ifdef ECONNRESET
                    143:                } else if (socerrno == ECONNRESET) {
2.29    ! frystyk   144:                    HTTRACE(STREAM_TRACE, "Read Socket. got ECONNRESET\n" _ soc);
2.27      frystyk   145:                    goto socketClosed;
                    146: #endif /* ECONNRESET */
2.1       frystyk   147:                } else {                             /* We have a real error */
                    148: 
2.9       frystyk   149:                    if (request)
                    150:                        HTRequest_addSystemError(request, ERR_FATAL, socerrno,
                    151:                                                 NO, "NETREAD");
2.1       frystyk   152:                    return HT_ERROR;
                    153:                }
2.9       frystyk   154:            } else if (!me->b_read) {
2.27      frystyk   155: 
2.9       frystyk   156:            socketClosed:
2.29    ! frystyk   157:                HTTRACE(STREAM_TRACE, "Read Socket. FIN received on socket %d\n" _ soc);
2.9       frystyk   158:                HTHost_unregister(host, net, HTEvent_READ);
2.18      frystyk   159:                HTHost_register(host, net, HTEvent_CLOSE);
2.1       frystyk   160:                return HT_CLOSED;
                    161:            }
                    162: 
                    163:            /* Remember how much we have read from the input socket */
2.29    ! frystyk   164:            HTTRACEDATA(me->data, me->b_read, "Reading from socket %d" _ soc);
2.1       frystyk   165:            me->write = me->data;
2.9       frystyk   166:            me->read = me->data + me->b_read;
2.13      eric      167: #ifdef FIND_SIGNATURES
                    168:            {
                    169:                char * ptr = me->data;
                    170:                int len = me->b_read;
                    171:                while ((ptr = strnstr(ptr, &len, "HTTP/1.1 200 OK")) != NULL) {
2.29    ! frystyk   172:                    HTTRACE(STREAM_TRACE, "Read Socket. Signature found at 0x%x of 0x%x.\n" _ ptr - me->data _ me->b_read);
2.13      eric      173:                    ptr++;
                    174:                    len--;
                    175:                }
                    176:            }
                    177: #endif /* FIND_SIGNATURES */
2.1       frystyk   178: #ifdef NOT_ASCII
                    179:            {
                    180:                char *p = me->data;
                    181:                while (p < me->read) {
                    182:                    *p = FROMASCII(*p);
                    183:                    p++;
                    184:                }
                    185:            }
                    186: #endif /* NOT_ASCII */
                    187: 
2.29    ! frystyk   188:            HTTRACE(STREAM_TRACE, "Read Socket. %d bytes read from socket %d\n" _ 
        !           189:                        me->b_read _ soc);
2.9       frystyk   190:            if (request) {
2.1       frystyk   191:                HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
2.26      frystyk   192:                if (HTNet_rawBytesCount(net))
                    193:                    HTNet_addBytesRead(net, me->b_read);
2.28      frystyk   194:                if (cbf) {
                    195:                    int tr = HTNet_bytesRead(net);
                    196:                    (*cbf)(request, HT_PROG_READ, HT_MSG_NULL, NULL, &tr, NULL);
                    197:                }
2.1       frystyk   198:            }
                    199:        }
                    200: 
                    201:        /* Now push the data down the stream */
2.9       frystyk   202:        if ((status = (*net->readStream->isa->put_block)
                    203:             (net->readStream, me->write, me->b_read)) != HT_OK) {
2.1       frystyk   204:            if (status == HT_WOULD_BLOCK) {
2.29    ! frystyk   205:                HTTRACE(STREAM_TRACE, "Read Socket. Target WOULD BLOCK\n");
2.9       frystyk   206:                HTHost_unregister(host, net, HTEvent_READ);
2.1       frystyk   207:                return HT_WOULD_BLOCK;
                    208:            } else if (status == HT_PAUSE) {
2.29    ! frystyk   209:                HTTRACE(STREAM_TRACE, "Read Socket. Target PAUSED\n");
2.9       frystyk   210:                HTHost_unregister(host, net, HTEvent_READ);
2.1       frystyk   211:                return HT_PAUSE;
2.9       frystyk   212:            /* CONTINUE code or stream code means data was consumed */
                    213:            } else if (status == HT_CONTINUE || status > 0) {
                    214:                if (status == HT_CONTINUE) {
2.29    ! frystyk   215:                    HTTRACE(STREAM_TRACE, "Read Socket. CONTINUE\n");
2.9       frystyk   216:                } else
2.29    ! frystyk   217:                    HTTRACE(STREAM_TRACE, "Read Socket. Target returns %d\n" _ status);
2.9       frystyk   218: /*             me->write = me->read; */
2.1       frystyk   219:                return status;
                    220:            } else {                                 /* We have a real error */
2.29    ! frystyk   221:                HTTRACE(STREAM_TRACE, "Read Socket. Target ERROR %d\n" _ status);
2.1       frystyk   222:                return status;
                    223:            }
                    224:        }
2.9       frystyk   225:        me->write = me->read;
2.19      frystyk   226:        {
                    227:            int remaining = HTHost_remainingRead(host);
                    228:            if (remaining > 0) {
2.29    ! frystyk   229:                HTTRACE(STREAM_TRACE, "Read Socket. DIDN'T CONSUME %d BYTES: `%s\'\n" _ 
        !           230:                            remaining _ me->read);
2.19      frystyk   231:                HTHost_setConsumed(host, remaining);
                    232:            }
                    233:        }
2.1       frystyk   234:     } while (net->preemptive);
2.9       frystyk   235:     HTHost_register(host, net, HTEvent_READ);
2.1       frystyk   236:     return HT_WOULD_BLOCK;
                    237: }
                    238: 
                    239: /*
                    240: **     The difference between the close and the free method is that we don't
                    241: **     close the connection in the free method - we only call the free method
                    242: **     of the target stream. That way, we can keep the input stream as long 
                    243: **     as the channel itself.
                    244: */
                    245: PRIVATE int HTReader_close (HTInputStream * me)
                    246: {
                    247:     int status = HT_OK;
2.9       frystyk   248:     HTNet * net = HTHost_getReadNet(me->host);
                    249:     if (net && net->readStream) {
                    250:        if ((status = (*net->readStream->isa->_free)(net->readStream))==HT_WOULD_BLOCK)
2.1       frystyk   251:            return HT_WOULD_BLOCK;
2.9       frystyk   252:        net->readStream = NULL;
2.1       frystyk   253:     }
2.29    ! frystyk   254:     HTTRACE(STREAM_TRACE, "Socket read. FREEING....\n");
2.1       frystyk   255:     HT_FREE(me);
                    256:     return status;
                    257: }
                    258: 
2.9       frystyk   259: PUBLIC int HTReader_consumed (HTInputStream * me, size_t bytes)
                    260: {
                    261:     me->write += bytes;
                    262:     me->b_read -= bytes;
                    263:     HTHost_setRemainingRead(me->host, me->b_read);
                    264:     return HT_OK;
                    265: }
                    266: 
2.1       frystyk   267: PRIVATE const HTInputStreamClass HTReader =
                    268: {
                    269:     "SocketReader",
                    270:     HTReader_flush,
                    271:     HTReader_free,
                    272:     HTReader_abort,
                    273:     HTReader_read,
2.9       frystyk   274:     HTReader_close,
                    275:     HTReader_consumed
2.1       frystyk   276: }; 
                    277: 
                    278: /*
                    279: **     Create a new input read stream. Before we actually create it we check
                    280: **     to see whether we already have an input stream for this channel and if
                    281: **     so we just return that. This means that we can reuse input streams 
                    282: **     in persistent connections, for example.
                    283: */
2.9       frystyk   284: PUBLIC HTInputStream * HTReader_new (HTHost * host, HTChannel * ch,
                    285:                                     void * param, int mode)
2.1       frystyk   286: {
2.9       frystyk   287:     if (host && ch) {
2.1       frystyk   288:        HTInputStream * me = HTChannel_input(ch);
                    289:        if (me == NULL) {
                    290:            if ((me=(HTInputStream *) HT_CALLOC(1, sizeof(HTInputStream))) == NULL)
                    291:            HT_OUTOFMEM("HTReader_new");
                    292:            me->isa = &HTReader;
                    293:            me->ch = ch;
2.9       frystyk   294:            me->host = host;
2.29    ! frystyk   295:            HTTRACE(STREAM_TRACE, "Reader...... Created reader stream %p\n" _ me);
2.1       frystyk   296:        }
                    297:        return me;
                    298:     }
                    299:     return NULL;
                    300: }
2.9       frystyk   301: 

Webmaster