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