Annotation of libwww/Library/src/HTSocket.c, revision 2.21
2.1 frystyk 1: /* HTSocket.c
2: ** MANAGES READ AND WRITE TO AND FROM THE NETWORK
3: **
4: ** (c) COPYRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
6: **
7: **
8: ** HISTORY:
9: ** 6 June 95 HFN Spawned off from HTFormat
10: */
11:
12: /* Library Include files */
13: #include "tcp.h"
14: #include "HTUtils.h"
15: #include "HTString.h"
2.7 frystyk 16: #include "HTReqMan.h"
2.4 frystyk 17: #include "HTProt.h"
2.1 frystyk 18: #include "HTTCP.h"
19: #include "HTStream.h"
2.6 frystyk 20: #include "HTAlert.h"
2.1 frystyk 21: #include "HTFormat.h"
2.8 frystyk 22: #include "HTNetMan.h"
2.1 frystyk 23: #include "HTError.h"
24: #include "HTSocket.h" /* Implemented here */
25:
2.2 frystyk 26: struct _HTInputSocket {
2.7 frystyk 27: char buffer[INPUT_BUFFER_SIZE];
28: char * write; /* Last byte written */
29: char * read; /* Last byte read */
2.17 frystyk 30: SOCKET sockfd;
2.2 frystyk 31: };
32:
2.1 frystyk 33: struct _HTStream {
34: CONST HTStreamClass * isa;
35: };
36:
37: /* ------------------------------------------------------------------------- */
38: /* SOCKET INPUT BUFFERING */
39: /* ------------------------------------------------------------------------- */
40: /*
41: ** This code is used because one cannot in general open a
42: ** file descriptor for a socket.
43: **
44: ** The input file is read using the macro which can read from
45: ** a socket or a file, but this should not be used for files
46: ** as fopen() etc is more portable of course.
47: **
48: ** The input buffer size, if large will give greater efficiency and
49: ** release the server faster, and if small will save space on PCs etc.
50: */
51:
52:
53: /* Set up the buffering
54: **
55: ** These routines are public because they are in fact needed by
56: ** many parsers, and on PCs and Macs we should not duplicate
57: ** the static buffer area.
58: */
2.17 frystyk 59: PUBLIC HTInputSocket * HTInputSocket_new (SOCKET file_number)
2.1 frystyk 60: {
61: HTInputSocket *isoc = (HTInputSocket *)calloc(1, sizeof(*isoc));
62: if (!isoc) outofmem(__FILE__, "HTInputSocket_new");
2.7 frystyk 63: isoc->sockfd = file_number;
64: isoc->write = isoc->read = isoc->buffer;
2.1 frystyk 65: return isoc;
66: }
67:
2.12 frystyk 68: PUBLIC void HTInputSocket_free (HTInputSocket * me)
69: {
70: if (me) free(me);
71: }
72:
2.1 frystyk 73: /* Push data from a socket down a stream
74: ** -------------------------------------
75: **
76: ** This routine is responsible for creating and PRESENTING any
77: ** graphic (or other) objects described by the file. As this function
78: ** max reads a chunk of data on size INPUT_BUFFER_SIZE, it can be used
79: ** with both blocking or non-blocking sockets. It will always return to
80: ** the event loop, however if we are using blocking I/O then we get a full
81: ** buffer read, otherwise we get what's available.
82: **
83: ** Returns HT_LOADED if finished reading
2.3 frystyk 84: ** HT_OK if OK, but more to read
2.1 frystyk 85: ** HT_ERROR if error,
2.21 ! frystyk 86: ** HT_WOULD_BLOCK if read or write would block
! 87: ** HT_PAUSE if stream is paused
2.1 frystyk 88: */
2.10 frystyk 89: PUBLIC int HTSocketRead (HTRequest * request, HTNet * net)
2.1 frystyk 90: {
2.7 frystyk 91: HTInputSocket *isoc = net->isoc;
2.10 frystyk 92: HTStream *target = net->target;
2.7 frystyk 93: int b_read = isoc->read - isoc->buffer;
2.1 frystyk 94: int status;
2.7 frystyk 95: if (!isoc || isoc->sockfd==INVSOC) {
2.14 frystyk 96: if (PROT_TRACE) TTYPrint(TDEST, "Read Socket. Bad argument\n");
2.1 frystyk 97: return HT_ERROR;
98: }
99:
2.3 frystyk 100: /* Read from socket if we got rid of all the data previously read */
2.4 frystyk 101: do {
2.7 frystyk 102: if (isoc->write >= isoc->read) {
103: if ((b_read = NETREAD(isoc->sockfd, isoc->buffer,
2.4 frystyk 104: INPUT_BUFFER_SIZE)) < 0) {
2.1 frystyk 105: #ifdef EAGAIN
2.4 frystyk 106: if (socerrno==EAGAIN || socerrno==EWOULDBLOCK) /* POSIX */
2.1 frystyk 107: #else
2.11 frystyk 108: if (socerrno==EWOULDBLOCK) /* BSD */
109: #endif
110: {
111: if (PROT_TRACE)
2.14 frystyk 112: TTYPrint(TDEST, "Read Socket. WOULD BLOCK soc %d\n",
2.11 frystyk 113: isoc->sockfd);
114: HTEvent_Register(isoc->sockfd, request, (SockOps) FD_READ,
115: net->cbf, net->priority);
116: return HT_WOULD_BLOCK;
2.21 ! frystyk 117: #ifdef __svr4__
! 118: /*
! 119: ** In Solaris envirnoment, SIGPOLL is used to signal end of buffer for
! 120: ** /dev/audio. If your process is also doing a socket read, it will cause
! 121: ** an EINTR error. This error will cause the www library request to
! 122: ** terminate prematurly.
! 123: */
! 124: } else if (socerrno == EINTR) {
! 125: continue;
! 126: #endif
2.11 frystyk 127: } else { /* We have a real error */
2.20 frystyk 128: HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO,
129: "NETREAD");
2.11 frystyk 130: return HT_ERROR;
131: }
2.4 frystyk 132: } else if (!b_read) {
2.16 frystyk 133: HTAlertCallback *cbf = HTAlert_find(HT_PROG_DONE);
2.3 frystyk 134: if (PROT_TRACE)
2.16 frystyk 135: TTYPrint(TDEST,"Read Socket. Finished loading socket %d\n",
136: isoc->sockfd);
137: if(cbf)(*cbf)(request,HT_PROG_DONE,HT_MSG_NULL,NULL,NULL,NULL);
2.7 frystyk 138: HTEvent_UnRegister(isoc->sockfd, FD_READ);
2.21 ! frystyk 139: return HT_CLOSED;
2.1 frystyk 140: }
141:
2.4 frystyk 142: /* Remember how much we have read from the input socket */
2.7 frystyk 143: isoc->write = isoc->buffer;
144: isoc->read = isoc->buffer + b_read;
2.1 frystyk 145:
146: #ifdef NOT_ASCII
2.4 frystyk 147: {
2.7 frystyk 148: char *p = isoc->buffer;
149: while (p < isoc->read) {
2.4 frystyk 150: *p = FROMASCII(*p);
151: p++;
152: }
2.1 frystyk 153: }
154: #endif
2.3 frystyk 155: if (PROT_TRACE)
2.14 frystyk 156: TTYPrint(TDEST, "Read Socket. %d bytes read from socket %d\n",
2.7 frystyk 157: b_read, isoc->sockfd);
2.8 frystyk 158: net->bytes_read += b_read;
2.16 frystyk 159:
160: {
161: HTAlertCallback *cbf = HTAlert_find(HT_PROG_READ);
162: if (cbf)
163: (*cbf)(request, HT_PROG_READ, HT_MSG_NULL,NULL,NULL,NULL);
164: }
2.4 frystyk 165: }
166:
167: /* Now push the data down the stream */
2.7 frystyk 168: if ((status = (*target->isa->put_block)(target, isoc->buffer,
2.4 frystyk 169: b_read)) != HT_OK) {
170: if (status==HT_WOULD_BLOCK) {
171: if (PROT_TRACE)
2.14 frystyk 172: TTYPrint(TDEST, "Read Socket. Target WOULD BLOCK\n");
2.7 frystyk 173: HTEvent_UnRegister(isoc->sockfd, FD_READ);
2.4 frystyk 174: return HT_WOULD_BLOCK;
2.21 ! frystyk 175: } else if (status == HT_PAUSE) {
! 176: if (PROT_TRACE) TTYPrint(TDEST,"Read Socket. Target PAUSED\n");
! 177: HTEvent_UnRegister(isoc->sockfd, FD_READ);
! 178: return HT_PAUSE;
2.5 frystyk 179: } else if (status>0) { /* Stream specific return code */
180: if (PROT_TRACE)
2.17 frystyk 181: TTYPrint(TDEST, "Read Socket. Target returns %d\n",status);
2.10 frystyk 182: isoc->write = isoc->buffer + b_read;
2.5 frystyk 183: return status;
184: } else { /* We have a real error */
2.4 frystyk 185: if (PROT_TRACE)
2.14 frystyk 186: TTYPrint(TDEST, "Read Socket. Target ERROR\n");
2.4 frystyk 187: return status;
188: }
2.1 frystyk 189: }
2.7 frystyk 190: isoc->write = isoc->buffer + b_read;
2.20 frystyk 191: } while (net->preemptive);
2.10 frystyk 192: HTEvent_Register(isoc->sockfd, request, (SockOps) FD_READ,
193: net->cbf, net->priority);
2.1 frystyk 194: return HT_WOULD_BLOCK;
195: }
2.2 frystyk 196:
2.19 frystyk 197: /* HTSocket_DLLHackFopen
198: ** ---------------------
199: ** Work around the problem that an app can't open a file and have a dll
200: ** read from it!
201: */
202: #ifdef WWW_WIN_DLL
203: PUBLIC FILE * HTSocket_DLLHackFopen (const char * filename, const char * mode)
204: {
205: return (fopen(filename, mode));
206: }
207: #endif /* WWW_WIN_DLL */
208:
2.2 frystyk 209: /* Push data from an ANSI file descriptor down a stream
210: ** ----------------------------------------------------
211: **
212: ** This routine is responsible for creating and PRESENTING any
213: ** graphic (or other) objects described by the file.
214: **
215: ** Bugs: When we can wait on a file then this should also check interrupts!
216: **
217: ** Returns HT_LOADED if finished reading
218: ** HT_ERROR if error,
219: */
2.10 frystyk 220: PUBLIC int HTFileRead (HTRequest * request, HTNet * net, FILE * fp)
2.2 frystyk 221: {
2.10 frystyk 222: HTInputSocket *isoc = net->isoc;
223: HTStream *target = net->target;
2.2 frystyk 224: int b_read;
225: int status;
226: if (!fp) {
2.14 frystyk 227: if (PROT_TRACE) TTYPrint(TDEST, "Read File... Bad argument\n");
2.2 frystyk 228: return HT_ERROR;
229: }
230:
231: while(1) {
2.7 frystyk 232: if ((b_read = fread(isoc->buffer, 1, INPUT_BUFFER_SIZE, fp))==0){
2.2 frystyk 233: if (ferror(fp)) {
234: if (PROT_TRACE)
2.14 frystyk 235: TTYPrint(TDEST, "Read File... READ ERROR\n");
2.2 frystyk 236: } else
237: return HT_LOADED;
238: }
2.7 frystyk 239: isoc->write = isoc->buffer;
240: isoc->read = isoc->buffer + b_read;
2.2 frystyk 241: if (PROT_TRACE)
2.14 frystyk 242: TTYPrint(TDEST, "Read File... %d bytes read from file %p\n",
2.2 frystyk 243: b_read, fp);
244:
245: /* Now push the data down the stream (we use blocking I/O) */
2.7 frystyk 246: if ((status = (*target->isa->put_block)(target, isoc->buffer,
2.2 frystyk 247: b_read)) != HT_OK) {
248: if (PROT_TRACE)
2.14 frystyk 249: TTYPrint(TDEST, "Read File... Target ERROR\n");
2.2 frystyk 250: return status;
251: }
2.7 frystyk 252: isoc->write = isoc->buffer + b_read;
2.2 frystyk 253: }
254: }
255:
2.17 frystyk 256: /* HTLoadSocket
257: ** ------------
258: ** Given an open socket, this routine loads what ever is on the socket
259: **
260: ** On entry,
261: ** request This is the request structure
262: ** On Exit
263: ** returns HT_ERROR Error has occured in call back
264: ** HT_OK Call back was OK
265: */
266: PUBLIC int HTLoadSocket (SOCKET soc, HTRequest * request, SockOps ops)
267: {
2.18 frystyk 268: HTNet * net = NULL;
269: if (!request) return HT_ERROR;
2.17 frystyk 270: if (ops == FD_NONE) {
2.18 frystyk 271: HTNet * me;
2.17 frystyk 272: if (soc==INVSOC) {
273: if (PROT_TRACE) TTYPrint(TDEST, "Load Socket. invalid socket\n");
274: return HT_ERROR;
275: }
276: if (PROT_TRACE) TTYPrint(TDEST,"Load Socket. Loading socket %d\n",soc);
2.18 frystyk 277: me = HTNet_new(request, soc);
278: me->sockfd = soc;
279: me->target = request->output_stream;
280: me->isoc = HTInputSocket_new(soc);
281: net = me;
2.17 frystyk 282: } else if (ops == FD_CLOSE) { /* Interrupted */
283: HTNet_delete(request->net, HT_INTERRUPTED);
284: return HT_OK;
2.18 frystyk 285: } else
286: net = request->net;
287: if (!net) {
288: if (PROT_TRACE) TTYPrint(TDEST, "Load Socket. invalid argument\n");
289: return HT_ERROR;
2.17 frystyk 290: }
291:
292: /* In this load function we only have one state: READ */
293: {
294: int status = HTSocketRead(request, net);
295: if (status == HT_WOULD_BLOCK)
296: return HT_OK;
297: else if (status == HT_LOADED)
298: HTNet_delete(request->net, HT_LOADED);
299: else
300: HTNet_delete(request->net, HT_ERROR);
301: }
302: return HT_OK;
303: }
Webmaster