Annotation of libwww/Library/src/HTReader.c, revision 2.13
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.13 ! eric 6: ** @(#) $Id: HTReader.c,v 2.12 1996/12/07 19:49:09 frystyk 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.13 ! eric 80: char * strnstr(char * haystack, int *pLen, char * needle)
! 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: }
! 98:
2.9 frystyk 99: int DebugBufferSize = INPUT_BUFFER_SIZE;
100:
2.13 ! eric 101: #include "HTFakRed.c"
2.1 frystyk 102: PRIVATE int HTReader_read (HTInputStream * me)
103: {
2.9 frystyk 104: HTHost * host = me->host;
105: SOCKET soc = HTChannel_socket(me->ch);
106: HTNet * net = HTHost_getReadNet(host);
107: HTRequest * request = HTNet_request(net);
2.1 frystyk 108: int status;
109:
2.9 frystyk 110: /* me->b_read = me->read - me->data; */
2.1 frystyk 111: /* Read from socket if we got rid of all the data previously read */
112: do {
2.9 frystyk 113: /* don't read if we have to push unwritten data from last call */
2.1 frystyk 114: if (me->write >= me->read) {
2.9 frystyk 115: if ((me->b_read = NETREAD(soc, me->data, DebugBufferSize)) < 0) {
2.1 frystyk 116: #ifdef EAGAIN
117: if (socerrno==EAGAIN || socerrno==EWOULDBLOCK) /* POSIX */
118: #else
119: if (socerrno==EWOULDBLOCK) /* BSD */
120: #endif
121: {
122: if (PROT_TRACE)
123: HTTrace("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__
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: */
133: } else if (socerrno == EINTR) {
134: continue;
135: #endif /* __svr4__ */
2.9 frystyk 136: #ifdef EPIPE
137: } else if (socerrno == EPIPE) {
138: goto socketClosed;
139: #endif /* EPIPE */
2.1 frystyk 140: } else { /* We have a real error */
141:
142: /* HERE WE SHOULD RETURN target abort */
2.9 frystyk 143: if (request)
144: HTRequest_addSystemError(request, ERR_FATAL, socerrno,
145: NO, "NETREAD");
2.1 frystyk 146: return HT_ERROR;
147: }
2.8 frystyk 148: #ifdef ECONNRESET
2.9 frystyk 149: } else if (!me->b_read || socerrno==ECONNRESET) {
2.8 frystyk 150: #else
2.9 frystyk 151: } else if (!me->b_read) {
2.8 frystyk 152: #endif
2.9 frystyk 153: socketClosed:
2.11 frystyk 154: if (PROT_TRACE)
155: HTTrace("Read Socket. FIN received on socket %d\n", soc);
2.9 frystyk 156: if (request) {
157: HTAlertCallback *cbf = HTAlert_find(HT_PROG_DONE);
158: if (PROT_TRACE)
159: if (cbf) (*cbf)(request, HT_PROG_DONE,
160: HT_MSG_NULL, NULL, NULL, NULL);
161: }
162: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 163: return HT_CLOSED;
164: }
165:
166: /* Remember how much we have read from the input socket */
2.10 eric 167: HTTraceData(me->data, me->b_read, "HTReader_read me->data:");
2.1 frystyk 168: me->write = me->data;
2.9 frystyk 169: me->read = me->data + me->b_read;
2.13 ! eric 170: #ifdef FIND_SIGNATURES
! 171: {
! 172: char * ptr = me->data;
! 173: int len = me->b_read;
! 174: while ((ptr = strnstr(ptr, &len, "HTTP/1.1 200 OK")) != NULL) {
! 175: if (PROT_TRACE)
! 176: HTTrace("Read Socket. Signature found at 0x%x of 0x%x.\n", ptr - me->data, me->b_read);
! 177: ptr++;
! 178: len--;
! 179: }
! 180: }
! 181: #endif /* FIND_SIGNATURES */
2.1 frystyk 182: #ifdef NOT_ASCII
183: {
184: char *p = me->data;
185: while (p < me->read) {
186: *p = FROMASCII(*p);
187: p++;
188: }
189: }
190: #endif /* NOT_ASCII */
191:
192: if (PROT_TRACE)
193: HTTrace("Read Socket. %d bytes read from socket %d\n",
2.9 frystyk 194: me->b_read, soc);
195: if (request) {
2.1 frystyk 196: HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
2.9 frystyk 197: #if 0
198: /* byte account is done later */
199: net->bytesRead += me->b_read;
200: #endif
201: if (cbf) (*cbf)(request, HT_PROG_READ,
2.1 frystyk 202: HT_MSG_NULL, NULL, NULL, NULL);
203: }
204: }
205:
206: /* Now push the data down the stream */
2.9 frystyk 207: if ((status = (*net->readStream->isa->put_block)
208: (net->readStream, me->write, me->b_read)) != HT_OK) {
2.1 frystyk 209: if (status == HT_WOULD_BLOCK) {
210: if (PROT_TRACE) HTTrace("Read Socket. Target WOULD BLOCK\n");
2.9 frystyk 211: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 212: return HT_WOULD_BLOCK;
213: } else if (status == HT_PAUSE) {
214: if (PROT_TRACE) HTTrace("Read Socket. Target PAUSED\n");
2.9 frystyk 215: HTHost_unregister(host, net, HTEvent_READ);
2.1 frystyk 216: return HT_PAUSE;
2.9 frystyk 217: /* CONTINUE code or stream code means data was consumed */
218: } else if (status == HT_CONTINUE || status > 0) {
219: if (status == HT_CONTINUE) {
220: if (PROT_TRACE) HTTrace("Read Socket. CONTINUE\n");
221: } else
222: if (PROT_TRACE)
223: HTTrace("Read Socket. Target returns %d\n", status);
224: /* me->write = me->read; */
2.1 frystyk 225: return status;
226: } else { /* We have a real error */
227: if (PROT_TRACE) HTTrace("Read Socket. Target ERROR\n");
228: return status;
229: }
230: }
2.9 frystyk 231: me->write = me->read;
232: HTHost_setRemainingRead(host, 0);
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: }
253: if (PROT_TRACE) HTTrace("Socket read. FREEING....\n");
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.1 frystyk 294: }
295: return me;
296: }
297: return NULL;
298: }
2.9 frystyk 299:
Webmaster