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