Annotation of libwww/Library/src/HTReader.c, revision 2.31
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.31 ! kahan 6: ** @(#) $Id: HTReader.c,v 2.30 1999/04/04 00:13:21 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: /*
2.31 ! kahan 128: ** In Solaris envirnoment, SIGPOLL is used to signal end
! 129: ** of buffer for /dev/audio. If your process is also doing
! 130: ** a socket read, it will cause an EINTR error. This
! 131: ** error will cause the www library request to
2.27 frystyk 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.31 ! kahan 147: #ifdef _WINSOCKAPI_ /* windows */
! 148: /*
! 149: ** JK: added new tests here, based on the following text:
! 150: ** Under BSD Unixes, if the remote peer closes its
! 151: ** connection and your program is blocking on recv(), you
! 152: ** will get a 0 back from recv(). Winsock behaves the same
! 153: ** way, except that it can also return -1, with
! 154: ** WSAGetLastError() returning WSAECONNRESET,
! 155: ** WSAECONNABORTED or WSAESHUTDOWN, to signal the
! 156: ** detectable flavors of abnormal disconnections.
! 157: ** (from the Winsock Programmer's FAQ, Warren Young)
! 158: */
! 159: } else if (socerrno == ECONNABORTED) {
! 160: HTTRACE(STREAM_TRACE, "Read Socket. got ECONNABORTED\n" _ soc);
! 161: goto socketClosed;
! 162: } else if (socerrno == ESHUTDOWN) {
! 163: HTTRACE(STREAM_TRACE, "Read Socket. got ESHUTDOWN\n" _ soc);
! 164: goto socketClosed;
! 165: #endif /* _WINSOCKAPI */
2.1 frystyk 166: } else { /* We have a real error */
167:
2.9 frystyk 168: if (request)
169: HTRequest_addSystemError(request, ERR_FATAL, socerrno,
170: NO, "NETREAD");
2.1 frystyk 171: return HT_ERROR;
172: }
2.9 frystyk 173: } else if (!me->b_read) {
2.27 frystyk 174:
2.9 frystyk 175: socketClosed:
2.29 frystyk 176: HTTRACE(STREAM_TRACE, "Read Socket. FIN received on socket %d\n" _ soc);
2.9 frystyk 177: HTHost_unregister(host, net, HTEvent_READ);
2.18 frystyk 178: HTHost_register(host, net, HTEvent_CLOSE);
2.1 frystyk 179: return HT_CLOSED;
180: }
181:
182: /* Remember how much we have read from the input socket */
2.29 frystyk 183: HTTRACEDATA(me->data, me->b_read, "Reading from socket %d" _ soc);
2.1 frystyk 184: me->write = me->data;
2.9 frystyk 185: me->read = me->data + me->b_read;
2.13 eric 186: #ifdef FIND_SIGNATURES
187: {
188: char * ptr = me->data;
189: int len = me->b_read;
190: while ((ptr = strnstr(ptr, &len, "HTTP/1.1 200 OK")) != NULL) {
2.29 frystyk 191: HTTRACE(STREAM_TRACE, "Read Socket. Signature found at 0x%x of 0x%x.\n" _ ptr - me->data _ me->b_read);
2.13 eric 192: ptr++;
193: len--;
194: }
195: }
196: #endif /* FIND_SIGNATURES */
2.1 frystyk 197: #ifdef NOT_ASCII
198: {
199: char *p = me->data;
200: while (p < me->read) {
201: *p = FROMASCII(*p);
202: p++;
203: }
204: }
205: #endif /* NOT_ASCII */
206:
2.29 frystyk 207: HTTRACE(STREAM_TRACE, "Read Socket. %d bytes read from socket %d\n" _
208: me->b_read _ soc);
2.9 frystyk 209: if (request) {
2.1 frystyk 210: HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
2.26 frystyk 211: if (HTNet_rawBytesCount(net))
212: HTNet_addBytesRead(net, me->b_read);
2.28 frystyk 213: if (cbf) {
214: int tr = HTNet_bytesRead(net);
215: (*cbf)(request, HT_PROG_READ, HT_MSG_NULL, NULL, &tr, NULL);
216: }
2.1 frystyk 217: }
218: }
219:
220: /* Now push the data down the stream */
2.9 frystyk 221: if ((status = (*net->readStream->isa->put_block)
222: (net->readStream, me->write, me->b_read)) != HT_OK) {
2.1 frystyk 223: if (status == HT_WOULD_BLOCK) {
2.29 frystyk 224: HTTRACE(STREAM_TRACE, "Read Socket. Target WOULD BLOCK\n");
2.9 frystyk 225: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 226: return HT_WOULD_BLOCK;
227: } else if (status == HT_PAUSE) {
2.29 frystyk 228: HTTRACE(STREAM_TRACE, "Read Socket. Target PAUSED\n");
2.9 frystyk 229: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 230: return HT_PAUSE;
2.9 frystyk 231: /* CONTINUE code or stream code means data was consumed */
232: } else if (status == HT_CONTINUE || status > 0) {
233: if (status == HT_CONTINUE) {
2.29 frystyk 234: HTTRACE(STREAM_TRACE, "Read Socket. CONTINUE\n");
2.9 frystyk 235: } else
2.29 frystyk 236: HTTRACE(STREAM_TRACE, "Read Socket. Target returns %d\n" _ status);
2.9 frystyk 237: /* me->write = me->read; */
2.1 frystyk 238: return status;
239: } else { /* We have a real error */
2.29 frystyk 240: HTTRACE(STREAM_TRACE, "Read Socket. Target ERROR %d\n" _ status);
2.1 frystyk 241: return status;
242: }
243: }
2.9 frystyk 244: me->write = me->read;
2.19 frystyk 245: {
246: int remaining = HTHost_remainingRead(host);
247: if (remaining > 0) {
2.29 frystyk 248: HTTRACE(STREAM_TRACE, "Read Socket. DIDN'T CONSUME %d BYTES: `%s\'\n" _
249: remaining _ me->read);
2.19 frystyk 250: HTHost_setConsumed(host, remaining);
251: }
252: }
2.1 frystyk 253: } while (net->preemptive);
2.9 frystyk 254: HTHost_register(host, net, HTEvent_READ);
2.1 frystyk 255: return HT_WOULD_BLOCK;
256: }
257:
258: /*
259: ** The difference between the close and the free method is that we don't
260: ** close the connection in the free method - we only call the free method
261: ** of the target stream. That way, we can keep the input stream as long
262: ** as the channel itself.
263: */
264: PRIVATE int HTReader_close (HTInputStream * me)
265: {
266: int status = HT_OK;
2.9 frystyk 267: HTNet * net = HTHost_getReadNet(me->host);
268: if (net && net->readStream) {
269: if ((status = (*net->readStream->isa->_free)(net->readStream))==HT_WOULD_BLOCK)
2.1 frystyk 270: return HT_WOULD_BLOCK;
2.9 frystyk 271: net->readStream = NULL;
2.1 frystyk 272: }
2.29 frystyk 273: HTTRACE(STREAM_TRACE, "Socket read. FREEING....\n");
2.1 frystyk 274: HT_FREE(me);
275: return status;
276: }
277:
2.9 frystyk 278: PUBLIC int HTReader_consumed (HTInputStream * me, size_t bytes)
279: {
280: me->write += bytes;
281: me->b_read -= bytes;
282: HTHost_setRemainingRead(me->host, me->b_read);
283: return HT_OK;
284: }
285:
2.1 frystyk 286: PRIVATE const HTInputStreamClass HTReader =
287: {
288: "SocketReader",
289: HTReader_flush,
290: HTReader_free,
291: HTReader_abort,
292: HTReader_read,
2.9 frystyk 293: HTReader_close,
294: HTReader_consumed
2.1 frystyk 295: };
296:
297: /*
298: ** Create a new input read stream. Before we actually create it we check
299: ** to see whether we already have an input stream for this channel and if
300: ** so we just return that. This means that we can reuse input streams
301: ** in persistent connections, for example.
302: */
2.9 frystyk 303: PUBLIC HTInputStream * HTReader_new (HTHost * host, HTChannel * ch,
304: void * param, int mode)
2.1 frystyk 305: {
2.9 frystyk 306: if (host && ch) {
2.1 frystyk 307: HTInputStream * me = HTChannel_input(ch);
308: if (me == NULL) {
309: if ((me=(HTInputStream *) HT_CALLOC(1, sizeof(HTInputStream))) == NULL)
310: HT_OUTOFMEM("HTReader_new");
311: me->isa = &HTReader;
312: me->ch = ch;
2.9 frystyk 313: me->host = host;
2.29 frystyk 314: HTTRACE(STREAM_TRACE, "Reader...... Created reader stream %p\n" _ me);
2.1 frystyk 315: }
316: return me;
317: }
318: return NULL;
319: }
2.9 frystyk 320:
Webmaster