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