Annotation of libwww/Library/src/HTReader.c, revision 2.9
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.9 ! frystyk 6: ** @(#) $Id: HTReader.c,v 2.8.2.13 1996/11/26 23:22:23 eric 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.9 ! frystyk 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.9 ! frystyk 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.9 ! frystyk 32: int b_read;
2.1 frystyk 33: };
34:
35: /* ------------------------------------------------------------------------- */
36:
37: PRIVATE int HTReader_flush (HTInputStream * me)
38: {
2.9 ! frystyk 39: HTNet * net = HTHost_getReadNet(me->host);
! 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.9 ! frystyk 45: HTNet * net = HTHost_getReadNet(me->host);
! 46: if (net && net->readStream) {
! 47: int status = (*net->readStream->isa->_free)(net->readStream);
! 48: if (status == HT_OK) 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.9 ! frystyk 56: HTNet * net = HTHost_getReadNet(me->host);
! 57: if (net && net->readStream) {
! 58: int status = (*net->readStream->isa->abort)(net->readStream, NULL);
! 59: if (status != HT_IGNORE) 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: */
2.9 ! frystyk 80: int DebugBufferSize = INPUT_BUFFER_SIZE;
! 81:
! 82: /* #include fakeReader.c */
2.1 frystyk 83: PRIVATE int HTReader_read (HTInputStream * me)
84: {
2.9 ! frystyk 85: HTHost * host = me->host;
! 86: SOCKET soc = HTChannel_socket(me->ch);
! 87: HTNet * net = HTHost_getReadNet(host);
! 88: HTRequest * request = HTNet_request(net);
2.1 frystyk 89: int status;
90:
2.9 ! frystyk 91: /* me->b_read = me->read - me->data; */
2.1 frystyk 92: /* Read from socket if we got rid of all the data previously read */
93: do {
2.9 ! frystyk 94: /* don't read if we have to push unwritten data from last call */
2.1 frystyk 95: if (me->write >= me->read) {
2.9 ! frystyk 96: if ((me->b_read = NETREAD(soc, me->data, DebugBufferSize)) < 0) {
2.1 frystyk 97: #ifdef EAGAIN
98: if (socerrno==EAGAIN || socerrno==EWOULDBLOCK) /* POSIX */
99: #else
100: if (socerrno==EWOULDBLOCK) /* BSD */
101: #endif
102: {
103: if (PROT_TRACE)
104: HTTrace("Read Socket. WOULD BLOCK fd %d\n",soc);
2.9 ! frystyk 105: HTHost_register(host, net, HTEvent_READ);
2.1 frystyk 106: return HT_WOULD_BLOCK;
107: #ifdef __svr4__
108: /*
109: ** In Solaris envirnoment, SIGPOLL is used to signal end of buffer for
110: ** /dev/audio. If your process is also doing a socket read, it will cause
111: ** an EINTR error. This error will cause the www library request to
112: ** terminate prematurly.
113: */
114: } else if (socerrno == EINTR) {
115: continue;
116: #endif /* __svr4__ */
2.9 ! frystyk 117: #ifdef EPIPE
! 118: } else if (socerrno == EPIPE) {
! 119: goto socketClosed;
! 120: #endif /* EPIPE */
2.1 frystyk 121: } else { /* We have a real error */
122:
123: /* HERE WE SHOULD RETURN target abort */
2.9 ! frystyk 124: if (request)
! 125: HTRequest_addSystemError(request, ERR_FATAL, socerrno,
! 126: NO, "NETREAD");
2.1 frystyk 127: return HT_ERROR;
128: }
2.8 frystyk 129: #ifdef ECONNRESET
2.9 ! frystyk 130: } else if (!me->b_read || socerrno==ECONNRESET) {
2.8 frystyk 131: #else
2.9 ! frystyk 132: } else if (!me->b_read) {
2.8 frystyk 133: #endif
2.9 ! frystyk 134: socketClosed:
! 135: HTTrace("Read Socket. FIN received on socket %d\n", soc);
! 136: if (request) {
! 137: HTAlertCallback *cbf = HTAlert_find(HT_PROG_DONE);
! 138: if (PROT_TRACE)
! 139: if (cbf) (*cbf)(request, HT_PROG_DONE,
! 140: HT_MSG_NULL, NULL, NULL, NULL);
! 141: }
! 142: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 143: return HT_CLOSED;
144: }
145:
146: /* Remember how much we have read from the input socket */
2.9 ! frystyk 147: HTTraceData(me->data, me->b_read, "HTReader_read %d bytes:", me->b_read);
2.1 frystyk 148: me->write = me->data;
2.9 ! frystyk 149: me->read = me->data + me->b_read;
2.1 frystyk 150:
151: #ifdef NOT_ASCII
152: {
153: char *p = me->data;
154: while (p < me->read) {
155: *p = FROMASCII(*p);
156: p++;
157: }
158: }
159: #endif /* NOT_ASCII */
160:
161: if (PROT_TRACE)
162: HTTrace("Read Socket. %d bytes read from socket %d\n",
2.9 ! frystyk 163: me->b_read, soc);
! 164: if (request) {
2.1 frystyk 165: HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
2.9 ! frystyk 166: #if 0
! 167: /* byte account is done later */
! 168: net->bytesRead += me->b_read;
! 169: #endif
! 170: if (cbf) (*cbf)(request, HT_PROG_READ,
2.1 frystyk 171: HT_MSG_NULL, NULL, NULL, NULL);
172: }
173: }
174:
175: /* Now push the data down the stream */
2.9 ! frystyk 176: if ((status = (*net->readStream->isa->put_block)
! 177: (net->readStream, me->write, me->b_read)) != HT_OK) {
2.1 frystyk 178: if (status == HT_WOULD_BLOCK) {
179: if (PROT_TRACE) HTTrace("Read Socket. Target WOULD BLOCK\n");
2.9 ! frystyk 180: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 181: return HT_WOULD_BLOCK;
182: } else if (status == HT_PAUSE) {
183: if (PROT_TRACE) HTTrace("Read Socket. Target PAUSED\n");
2.9 ! frystyk 184: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 185: return HT_PAUSE;
2.9 ! frystyk 186: /* CONTINUE code or stream code means data was consumed */
! 187: } else if (status == HT_CONTINUE || status > 0) {
! 188: if (status == HT_CONTINUE) {
! 189: if (PROT_TRACE) HTTrace("Read Socket. CONTINUE\n");
! 190: } else
! 191: if (PROT_TRACE)
! 192: HTTrace("Read Socket. Target returns %d\n", status);
! 193: /* me->write = me->read; */
2.1 frystyk 194: return status;
195: } else { /* We have a real error */
196: if (PROT_TRACE) HTTrace("Read Socket. Target ERROR\n");
197: return status;
198: }
199: }
2.9 ! frystyk 200: me->write = me->read;
! 201: HTHost_setRemainingRead(host, 0);
2.1 frystyk 202: } while (net->preemptive);
2.9 ! frystyk 203: HTHost_register(host, net, HTEvent_READ);
2.1 frystyk 204: return HT_WOULD_BLOCK;
205: }
206:
207: /*
208: ** The difference between the close and the free method is that we don't
209: ** close the connection in the free method - we only call the free method
210: ** of the target stream. That way, we can keep the input stream as long
211: ** as the channel itself.
212: */
213: PRIVATE int HTReader_close (HTInputStream * me)
214: {
215: int status = HT_OK;
2.9 ! frystyk 216: HTNet * net = HTHost_getReadNet(me->host);
! 217: if (net && net->readStream) {
! 218: if ((status = (*net->readStream->isa->_free)(net->readStream))==HT_WOULD_BLOCK)
2.1 frystyk 219: return HT_WOULD_BLOCK;
2.9 ! frystyk 220: net->readStream = NULL;
2.1 frystyk 221: }
222: if (PROT_TRACE) HTTrace("Socket read. FREEING....\n");
223: HT_FREE(me);
224: return status;
225: }
226:
2.9 ! frystyk 227: PUBLIC int HTReader_consumed (HTInputStream * me, size_t bytes)
! 228: {
! 229: if (PROT_TRACE) HTTrace("Socket read. consumed %d bytes\n", bytes);
! 230: me->write += bytes;
! 231: me->b_read -= bytes;
! 232: HTHost_setRemainingRead(me->host, me->b_read);
! 233: return HT_OK;
! 234: }
! 235:
2.1 frystyk 236: PRIVATE const HTInputStreamClass HTReader =
237: {
238: "SocketReader",
239: HTReader_flush,
240: HTReader_free,
241: HTReader_abort,
242: HTReader_read,
2.9 ! frystyk 243: HTReader_close,
! 244: HTReader_consumed
2.1 frystyk 245: };
246:
247: /*
248: ** Create a new input read stream. Before we actually create it we check
249: ** to see whether we already have an input stream for this channel and if
250: ** so we just return that. This means that we can reuse input streams
251: ** in persistent connections, for example.
252: */
2.9 ! frystyk 253: PUBLIC HTInputStream * HTReader_new (HTHost * host, HTChannel * ch,
! 254: void * param, int mode)
2.1 frystyk 255: {
2.9 ! frystyk 256: if (host && ch) {
2.1 frystyk 257: HTInputStream * me = HTChannel_input(ch);
258: if (me == NULL) {
259: if ((me=(HTInputStream *) HT_CALLOC(1, sizeof(HTInputStream))) == NULL)
260: HT_OUTOFMEM("HTReader_new");
261: me->isa = &HTReader;
262: me->ch = ch;
2.9 ! frystyk 263: me->host = host;
2.1 frystyk 264: }
265: return me;
266: }
267: return NULL;
268: }
2.9 ! frystyk 269:
Webmaster