Annotation of OpenXM/src/kan96xx/plugin/sm1Socket.c, Revision 1.14
1.14 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.13 2002/10/24 02:12:35 takayama Exp $ */
1.1 maekawa 2: /* msg0s.c */
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <sys/socket.h>
6: #include <sys/time.h>
7: #include <netinet/in.h>
8: #include <netdb.h>
9:
10:
11: #include "datatype.h"
12: #include "stackm.h"
13: #include "extern.h"
14: #include "sm1Socket.h"
15:
16: extern int Quiet;
1.14 ! takayama 17: static int Post_debug=0;
1.1 maekawa 18: static void errorMsg1s(char *s);
1.8 takayama 19: static int getContentLength(char *s);
20: static int getReceivedContentLength(char *s);
1.6 takayama 21: #define MAX_LISTEN_QUEUE 3
1.1 maekawa 22:
23: /* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */
24: struct object KsocketOpen(struct object obj) {
25: char serverName[1024];
26: int portNumber;
27: struct object rob = NullObject;
28: struct hostent *myhost;
29: struct sockaddr_in me;
30: int s_waiting;
31: int on;
32: int tt;
33: extern int errno;
1.14 ! takayama 34: extern int Post_debug;
! 35:
! 36: if ((char *)getenv("OXWEB_DEBUG") != NULL) {
! 37: Post_debug = 1;
! 38: }
1.1 maekawa 39:
40: if (obj.tag != Sarray) {
41: errorMsg1s("KsocketOpen([optional integer,optional string name])");
42: }
43: strcpy(serverName,"localhost");
44: portNumber = 0;
45: if (getoaSize(obj) >= 1) {
46: if ((getoa(obj,0)).tag != Sinteger) {
47: errorMsg1s("KsocketOpen([optional integer,optional string name]), the first argument must be an integer.");
48: }
49: portNumber = KopInteger(getoa(obj,0));
50: }
51: if (getoaSize(obj) >= 2) {
52: if ((getoa(obj,1)).tag != Sdollar) {
53: errorMsg1s("KsocketOpen([optional integer,optional string name]), the second argument must be a string.");
54: }
55: if (strlen(KopString(getoa(obj,1))) > 1023) {
56: errorMsg1s("Too long server name");
57: }
58: strcpy(serverName,KopString(getoa(obj,1)));
59: }
60:
61:
62: /* fprintf(stderr,"Hello from open.\n"); */
63: if ((myhost = gethostbyname(serverName)) == NULL) {
64: errorMsg1s("Bad server name.");
65: }
66: bzero((char *)&me,sizeof(me));
67: me.sin_family = AF_INET;
68: me.sin_port = htons(portNumber);
69: bcopy(myhost->h_addr,
1.3 takayama 70: &me.sin_addr,myhost->h_length);
1.1 maekawa 71:
72: if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
73: errorMsg1s("Socket allocation is failed.");
74: }
75:
76: on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
77: /* important */
78: if (bind(s_waiting,(struct sockaddr *) &me,sizeof(me)) == -1) {
79: fprintf(stderr,"Bind error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
80: errorMsg1s("cannot bind");
81: }
82:
83: tt = sizeof(me);
84: if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
85: fprintf(stderr,"getsockname error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
86: errorMsg1s("cannot getsockname");
87: }
88:
89:
1.6 takayama 90: if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.1 maekawa 91: errorMsg1s("Listen failed");
92: }
93: /*
1.3 takayama 94: fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port));
95: fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */
1.1 maekawa 96: rob = newObjectArray(2);
97: putoa(rob,0,KpoInteger(s_waiting));
98: putoa(rob,1,KpoInteger(ntohs(me.sin_port)));
99: return(rob);
100:
101: }
102:
103: /* [ integer port, optional string host default localhost]
104: [ integer socketid, integer port ] */
105: struct object KsocketConnect(struct object obj) {
106: char serverName[1024];
107: int portNumber;
108: struct hostent *servhost;
109: struct sockaddr_in server;
110: int socketid;
111: struct object robj = NullObject;
112:
113: if (obj.tag != Sarray) {
114: errorMsg1s("KsocketConnect( [ integer port, optional string host default localhost])");
115: }
116: strcpy(serverName,"localhost");
117: if (getoaSize(obj) < 1) {
118: errorMsg1s("You need to specify port number.\n");
119: }
120: if (getoaSize(obj) >= 1) {
121: if ((getoa(obj,0)).tag != Sinteger) {
122: errorMsg1s("KsocketConnect([integer port,optional string host]), the first argument must be an integer.");
123: }
124: portNumber = KopInteger(getoa(obj,0));
125: }
126: if (getoaSize(obj) >= 2) {
127: if ((getoa(obj,1)).tag != Sdollar) {
128: errorMsg1s("KsocketConnect([integer port,optional string host]), the second argument must be a string.");
129: }
130: if (strlen(KopString(getoa(obj,1))) > 1023) {
131: errorMsg1s("Too long server name");
132: }
133: strcpy(serverName,KopString(getoa(obj,1)));
134: }
135:
136:
137: if ((servhost = gethostbyname(serverName)) == NULL) {
138: errorMsg1s("bad server name.\n");
139: }
140: bzero((char *)&server,sizeof(server));
141: server.sin_family = AF_INET;
142: server.sin_port = htons(portNumber);
143: bcopy(servhost->h_addr,
1.3 takayama 144: (char *)&server.sin_addr,servhost->h_length);
1.1 maekawa 145:
146: if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
147: errorMsg1s("socket allocation is failed.\n");
148: }
149: if (!Quiet) {
150: fprintf(stderr,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
151: }
152: if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
153: errorMsg1s("cannot connect");
154: }
155: /* fprintf(stderr,"connected.\n"); */
156: robj = newObjectArray(2);
157: putoa(robj,0,KpoInteger(socketid));
158: putoa(robj,1,KpoInteger(portNumber));
159: return(robj);
160: }
161:
162: /* [ integer socketid ]
163: [ integer newsocketid ] */
164: struct object KsocketAccept(struct object obj) {
165: struct object obj1;
166: struct object obj2;
167: struct object robj;
168: int s, news;
169:
170: if (obj.tag != Sarray) {
171: errorMsg1s("KsocketAccept([integer socketid])");
172: }
173: if (getoaSize(obj) < 1) {
174: errorMsg1s("KsocketAccept([integer socketid])");
175: }
176: obj1 = getoa(obj,0);
177: if (obj1.tag != Sinteger ) {
178: errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
179: }
180: s = KopInteger(obj1);
181: if ((news = accept(s,NULL,NULL)) < 0) {
182: errorMsg1s("Error in accept.");
183: }
184: if (close(s) < 0) {
185: errorMsg1s("Error in closing the old socket.");
186: }
187: robj = newObjectArray(1);
188: putoa(robj,0,KpoInteger(news));
189: return(robj);
190: }
191:
1.13 takayama 192: /* [ integer socketid ]
193: [ integer newsocketid ] */
194: /* It does not close the listening socket. You can call it as
195: ls = open.
196: fd=accept2(ls). close(fd).
197: fd=accept2(ls). close(fd).
198: ....
199: */
200: struct object KsocketAccept2(struct object obj) {
201: struct object obj1;
202: struct object obj2;
203: struct object robj;
204: int s, news;
205:
206: if (obj.tag != Sarray) {
207: errorMsg1s("KsocketAccept([integer socketid])");
208: }
209: if (getoaSize(obj) < 1) {
210: errorMsg1s("KsocketAccept([integer socketid])");
211: }
212: obj1 = getoa(obj,0);
213: if (obj1.tag != Sinteger ) {
214: errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
215: }
216: s = KopInteger(obj1);
217: if ((news = accept(s,NULL,NULL)) < 0) {
218: errorMsg1s("Error in accept.");
219: }
220: if (close(s) < 0) {
221: errorMsg1s("Error in closing the old socket.");
222: }
223: robj = newObjectArray(1);
224: putoa(robj,0,KpoInteger(news));
225: return(robj);
226: }
227:
1.1 maekawa 228: int KsocketSelect0(int fd,int t) {
229: fd_set readfds;
230: struct timeval timeout;
231: extern int errno;
232: FD_ZERO(&readfds);
233: FD_SET(fd,&readfds);
234: timeout.tv_sec = 0;
235: timeout.tv_usec = (long) t;
236: if (t >= 0) {
237: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
238: /* It must be fd+1 !, Not fd. */
239: fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
240: errorMsg1s("KsocketSelect0() : select failed.");
241: return(0);
242: }
243: }else{ /* block */
244: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
245: errorMsg1s("KsocketSelect0() : select failed.");
246: fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
247: return(0);
248: }
249: }
250: if (FD_ISSET(fd,&readfds)) return(1);
251: else return(0);
252: }
253:
254: /* [ integer socketid optional integer timeout default 0]
255: integer true or false
256: */
257: struct object KsocketSelect(struct object obj) {
258: struct object robj;
259: struct object ob1;
260: struct object ob2;
261: if (obj.tag != Sarray) {
262: errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
263: }
264: if (getoaSize(obj) < 1) {
265: errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
266: }
267: if (getoaSize(obj) >= 1) {
268: ob1 = getoa(obj,0);
269: ob2 = KpoInteger(0); /* default value */
270: }
271: if (getoaSize(obj) >= 2) {
272: ob2 = getoa(obj,1);
273: }
274: if (ob1.tag != Sinteger) {
275: errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the first argument must be an integer.");
276: }
277: if (ob2.tag != Sinteger) {
278: errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the second argument must be an integer.");
279: }
280: robj = KpoInteger( KsocketSelect0(KopInteger(ob1),KopInteger(ob2)) );
281: return(robj);
282: }
283:
284: struct object KsocketSelectMulti(struct object obj)
1.3 takayama 285: /* [ [integer socketid1, integer socketid2, ...]
1.1 maekawa 286: optional integer timeout default 0]
287: [ result1, result2, ....]
288: */
289: {
290: struct object robj;
291: struct object ob1;
292: struct object ob2;
293: struct object ob3;
294: int size,i,fd,p,t;
295: fd_set readfds;
296: struct timeval timeout;
297: extern errno;
298: if (obj.tag != Sarray) {
299: errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
300: }
301: if (getoaSize(obj) < 1) {
302: errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
303: }
304: if (getoaSize(obj) >= 1) {
305: ob1 = getoa(obj,0);
306: ob2 = KpoInteger(0); /* default value */
307: }
308: if (getoaSize(obj) >= 2) {
309: ob2 = getoa(obj,1);
310: }
311: if (ob1.tag != Sarray) {
312: errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the first argument must be an array.");
313: }
314: if (ob2.tag != Sinteger) {
315: errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the second argument must be an integer.");
316: }
317: FD_ZERO(&readfds);
318: timeout.tv_sec = 0; t = KopInteger(ob2);
319: timeout.tv_usec = (long)t;
320:
321: size = getoaSize(ob1);
322: if (size < 1) {
323: errorMsg1s("KsocketSelectMulti: the first argument must be a non-empty array of integers.");
324: }
325: fd = 0;
326:
327: for (i=0; i<size; i++) {
328: ob3 = getoa(ob1,i);
329: if (ob3.tag != Sinteger) {
330: errorMsg1s("KsocketSelectMulti: the first argument must be an array of integers.");
331: }
332: p = KopInteger(ob3);
333: if (p > fd) fd = p;
334: FD_SET(p,&readfds);
335: /* printf("p = %d, fd=%d",p,fd); */
336: }
337:
338: if (t >= 0) {
339: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
340: /* It must be fd+1 !, Not fd. */
341: fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
342: errorMsg1s("KsocketSelectMulti() : select failed.");
343: }
344: }else{ /* block */
345: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
346: fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
347: errorMsg1s("KsocketSelectMulti() : (block) select failed.");
348: }
349: }
350: robj = newObjectArray(size);
351: for (i=0; i<size; i++) {
352: if (FD_ISSET(KopInteger(getoa(ob1,i)),&readfds)) {
353: putoa(robj,i,KpoInteger(1));
354: }else{
355: putoa(robj,i,KpoInteger(0));
356: }
357: }
358:
359: return(robj);
360: }
361:
362:
363:
364:
365: static char Data00[1024];
366: /* [ integer socketid ]
367: string data
368: */
369: struct object KsocketRead(struct object obj) {
370: struct object ob1;
371: struct object robj = NullObject;
372: static int datasize = 1024;
373: static char *data = Data00;
374: char *tmp;
375: char *r;
376: int n;
377: int socketid;
378: int totalsize;
379:
380: if (obj.tag != Sarray) {
381: errorMsg1s("KsocketRead([integer socketid])");
382: }
383: if (getoaSize(obj) < 1) {
384: errorMsg1s("KsocketRead([integer socketid])");
385: }
386: ob1 = getoa(obj,0);
387: if (ob1.tag != Sinteger) {
388: errorMsg1s("KsocketRead([integer socketid]) : the argument must be integer.");
389: }
390: socketid = KopInteger(ob1);
391:
392: totalsize = 0;
393:
394: while (KsocketSelect0(socketid,0)) {
395: if (datasize - totalsize > 0) {
396: n = read(socketid,data+totalsize,datasize-totalsize);
397: if (n < 0) {
1.3 takayama 398: errorMsg1s("Read error.");
1.1 maekawa 399: }
400: if (n < datasize-totalsize) {
1.3 takayama 401: totalsize += n;
402: break;
1.1 maekawa 403: }else{ totalsize += n; }
404: if (totalsize == 0) {
1.3 takayama 405: errorMsg1s("Select returns 1, but there is no data to read.");
1.1 maekawa 406: }
407: }else { /* increase the datasize */
408: tmp = (char *)GC_malloc(sizeof(char)*2*datasize);
409: /*I should use GC_malloc_atomic and free after finishing this function?*/
410: if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
411: bcopy(data,tmp,totalsize);
412: data = tmp;
413: datasize = 2*datasize;
414: }
415: }
416:
1.5 takayama 417: r = (char *)GC_malloc(sizeof(char)*(totalsize+1));
1.1 maekawa 418: if (r == (char *)NULL) errorMsg1s("Out of Memory.");
419: bcopy(data,r,totalsize);
1.5 takayama 420: r[totalsize] = 0;
421: robj = KpoString(r); /* BUG: it works only for reading string from TCP/IP
422: stream. */
1.1 maekawa 423:
424: return(robj);
425:
426: }
427:
428: /* [ integer socketid, string data ]
429: integer */
430: struct object KsocketWrite(struct object obj) {
431: struct object ob1;
432: struct object ob2;
433: int socketid;
434: int r;
435: if (obj.tag != Sarray) {
436: errorMsg1s("KsocketWrite([integer socketid, string data])");
437: }
438: if (getoaSize(obj) < 2) {
439: errorMsg1s("KsocketWrite([integer socketid, string data])");
440: }
441: ob1 = getoa(obj,0);
442: ob2 = getoa(obj,1);
443: if (ob1.tag != Sinteger) {
444: errorMsg1s("KsocketWrite([integer socketid, string data]) : the first argument must be an integer.");
445: }
446: socketid = KopInteger(ob1);
447: if (ob2.tag != Sdollar) {
448: errorMsg1s("KsocketWrite([integer socketid, string data]) : the second argument must be a string.");
449: }
450: r = write(socketid,KopString(ob2), strlen(KopString(ob2)));
451: return(KpoInteger(r));
452:
453: }
454: struct object KsocketClose(struct object obj) {
455: int socketid;
456: struct object ob1;
457: if (obj.tag != Sarray) {
458: errorMsg1s("KsocketClose([ integer socketid ])");
459: }
460: if (getoaSize(obj) != 1) {
461: errorMsg1s("KsocketClose([ integer socketid ])");
462: }
463: ob1 = getoa(obj,0);
464: if (ob1.tag != Sinteger) {
465: errorMsg1s("KsocketClose([ INTEGER socketid ])");
466: }
467: socketid = KopInteger(ob1);
468: return(KpoInteger(close(socketid)));
469: }
470:
471:
472: static void errorMsg1s(char *s) {
473: fprintf(stderr,"%s\n",s);
474: errorKan1("msg1s.c: %s\n",s);
475: }
476:
477:
478: /**************** new 1997, 11/23 *******************/
479: struct object KsocketReadByte(struct object obj);
480: struct object KsocketWriteByte(struct object obj);
481:
482: struct object KsocketReadByte(struct object obj) {
483: struct object ob1;
484: struct object robj = NullObject;
485: char data[2];
486: char *tmp;
487: char *r;
488: int n;
489: int socketid;
490:
491:
492: if (obj.tag != Sarray) {
493: errorMsg1s("KsocketReadByte([integer socketid])");
494: }
495: if (getoaSize(obj) < 1) {
496: errorMsg1s("KsocketReadByte([integer socketid])");
497: }
498: ob1 = getoa(obj,0);
499: if (ob1.tag != Sinteger) {
500: errorMsg1s("KsocketReadByte([integer socketid]) : the argument must be integer.");
501: }
502: socketid = KopInteger(ob1);
503:
504:
505: n = read(socketid,data,1);
506: if (n < 0) {
507: errorMsg1s("Read error.");
508: robj = KpoInteger(-1);
509: return(robj);
510: }
511: if (n == 0) {
512: errorMsg1s("Read returned without data.");
513: }
514:
515: robj = KpoInteger((int)((unsigned char)data[0]));
516: return(robj);
517:
518: }
519:
520: /* [ integer socketid, int ]
521: integer */
522: struct object KsocketWriteByte(struct object obj) {
523: struct object ob1;
524: struct object ob2;
525: int socketid;
1.4 takayama 526: int r,i,n,kk,r0;
527: #define DATA_SIZE 1024
528: char data[DATA_SIZE];
1.1 maekawa 529: if (obj.tag != Sarray) {
1.4 takayama 530: errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1 maekawa 531: }
532: if (getoaSize(obj) < 2) {
1.4 takayama 533: errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1 maekawa 534: }
535: ob1 = getoa(obj,0);
536: ob2 = getoa(obj,1);
537: if (ob1.tag != Sinteger) {
1.4 takayama 538: errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer.");
1.1 maekawa 539: }
540: socketid = KopInteger(ob1);
1.4 takayama 541: if (ob2.tag != Sinteger && ob2.tag != Sarray) {
542: errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers.");
543: }
544: if (ob2.tag == Sinteger) {
545: data[0] = KopInteger(ob2);
546: r = write(socketid,data, 1);
547: }else{
548: n = getoaSize(ob2); kk = 0; r = 0;
549: for (i=0; i<n; i++) {
550: if (getoa(ob2,i).tag != Sinteger)
551: errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers.");
552: data[kk] = KopInteger(getoa(ob2,i));
553: kk++;
554: if (kk >= DATA_SIZE) {
555: r0 = write(socketid,data,kk);
556: if (r0 != kk) {
557: fprintf(stderr,"Warning: Could not write to the socket.\n");
558: return(KpoInteger(r+r0));
559: }
560: r += r0;
561: kk = 0;
562: }
563: }
564: if (kk > 0) {
565: r0 = write(socketid,data,kk);
566: if (r0 != kk) {
567: fprintf(stderr,"Warning: Could not write to the socket.\n");
568: return(KpoInteger(r+r0));
569: }
570: r += r0;
571: }
1.1 maekawa 572: }
573: return(KpoInteger(r));
574: }
575:
1.7 takayama 576: struct object KsocketReadHTTP(struct object socketObj) {
577: /* Read until two empty line appears. */
1.5 takayama 578: struct object ob;
1.13 takayama 579: struct object ob1;
580: struct object nob;
1.5 takayama 581: char *s;
582: char *sss;
583: char *tmp;
584: int i;
585: int flag;
1.7 takayama 586: int flagmax = 1;
1.5 takayama 587: int datasize;
1.7 takayama 588: int last;
1.8 takayama 589: int contentLength=-1;
1.13 takayama 590: int socketid;
1.14 ! takayama 591: extern int Post_debug;
1.13 takayama 592: nob = NullObject;
593:
594: if (socketObj.tag != Sarray) {
595: errorMsg1s("KsocketReadHTTP([integer socketid])");
596: }
597: if (getoaSize(socketObj) < 1) {
598: errorMsg1s("KsocketReadHTTP([integer socketid])");
599: }
600: ob1 = getoa(socketObj,0);
601: if (ob1.tag != Sinteger) {
602: errorMsg1s("KsocketReadHTTP([integer socketid]) : the argument must be integer.");
603: }
604: socketid = KopInteger(ob1);
605:
606: if (KsocketSelect0(socketid,-1) != 1) {
607: return(nob);
608: }
1.5 takayama 609: ob = KsocketRead(socketObj);
610: s = KopString(ob);
1.8 takayama 611: if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */
1.7 takayama 612: else flagmax=1;
1.5 takayama 613: flag = 0;
614: for (i=strlen(s)-1; i>=0; i--) {
615: if ((s[i] == '\n') && (i==0)) {
1.7 takayama 616: ++flag;
1.5 takayama 617: }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7 takayama 618: ++flag;
1.5 takayama 619: }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) {
1.7 takayama 620: ++flag;
1.5 takayama 621: }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7 takayama 622: ++flag;
1.5 takayama 623: }
624: }
1.7 takayama 625: if (flag >= flagmax) return ob;
1.5 takayama 626: datasize = strlen(s);
627: sss = s;
1.7 takayama 628: if ((s[strlen(s)-1] == '\n') ||
629: (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
630: last = 1;
631: }else last = 0;
1.5 takayama 632:
1.7 takayama 633: while (flag < flagmax) {
1.8 takayama 634: contentLength = getContentLength(sss);
635: if (contentLength != -1) {
636: if (contentLength <= getReceivedContentLength(sss)) {
637: break;
638: }
639: }
1.14 ! takayama 640: if (Post_debug) {
! 641: fprintf(stderr,"Waiting in socketReadBlock. flagmax(0d,0a)=%d, content-length=%d, received content-length=%d\n",flagmax,contentLength,getReceivedContentLength(sss));
! 642: }
1.13 takayama 643: if (strlen(s) == 0) {
644: fprintf(stderr,"No data. Perhaps connection is closed by foreign host.\n");
645: return nob;
1.5 takayama 646: }else{
647: /* for debugging. */
1.14 ! takayama 648: if (Post_debug) {
! 649: for (i=0; i<strlen(sss); i++) {
! 650: if ((sss[i] >= ' ') && (sss[i] < 0x7f)) {
! 651: fprintf(stderr,"%c",sss[i]);
! 652: }else{
! 653: fprintf(stderr,"(%3x)",sss[i]);
! 654: if (sss[i] == 0xa) fprintf(stderr,"\n");
! 655: }
! 656:
1.8 takayama 657: }
1.14 ! takayama 658: fprintf(stderr,"\n");
1.5 takayama 659: }
660: }
1.13 takayama 661:
662: if (KsocketSelect0(socketid,-1) != 1) {
663: return nob;
664: }
1.5 takayama 665: ob = KsocketRead(socketObj);
666: s = KopString(ob);
667: for (i=strlen(s)-1; i>=0; i--) {
1.7 takayama 668: if ((s[i] == '\n') && (i==0) && last) {
669: ++flag;
1.5 takayama 670: }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7 takayama 671: ++flag;
672: }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i==0) && last) {
673: ++flag;
1.5 takayama 674: }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7 takayama 675: ++flag;
1.5 takayama 676: }
677: }
678: if (datasize-1 <= strlen(sss)+strlen(s)) {
679: tmp = (char *)GC_malloc(sizeof(char)*2*(datasize+strlen(s))+1);
680: if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
681: strcpy(tmp,sss);
682: strcat(tmp,s);
683: datasize = 2*(datasize+strlen(s));
684: sss = tmp;
685: }else{
686: strcat(sss,s);
687: }
1.7 takayama 688:
689: if ((s[strlen(s)-1] == '\n') ||
690: (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
691: last = 1;
692: }else last = 0;
693:
1.5 takayama 694: }
695:
696: return KpoString(sss);
697:
698: }
699:
1.1 maekawa 700: struct object Kplugin_sm1Socket(char *key,struct object obj) {
701: struct object robj = NullObject;
702: if (strcmp(key,"open") == 0) {
703: robj = KsocketOpen(obj);
704: }else if (strcmp(key,"connect") == 0) {
705: robj = KsocketConnect(obj);
706: }else if (strcmp(key,"accept") == 0) {
707: robj = KsocketAccept(obj);
1.13 takayama 708: }else if (strcmp(key,"accept2") == 0) {
709: robj = KsocketAccept2(obj);
1.1 maekawa 710: }else if (strcmp(key,"select") == 0) {
711: robj = KsocketSelect(obj);
712: }else if (strcmp(key,"mselect") == 0) {
713: robj = KsocketSelectMulti(obj);
714: }else if (strcmp(key,"read") == 0) {
715: robj = KsocketRead(obj);
1.7 takayama 716: }else if (strcmp(key,"readHTTP") == 0) {
717: robj = KsocketReadHTTP(obj);
1.11 takayama 718: }else if (strcmp(key,"gethostname") == 0) {
719: robj = KsocketGetHostName();
720: }else if (strcmp(key,"write") == 0) {
1.1 maekawa 721: robj = KsocketWrite(obj);
722: }else if (strcmp(key,"read") == 0) {
723: robj = KsocketRead(obj);
724: }else if (strcmp(key,"readByte") == 0) {
725: robj = KsocketReadByte(obj);
726: }else if (strcmp(key,"writeByte") == 0) {
727: robj = KsocketWriteByte(obj);
728: }else if (strcmp(key,"close") == 0) {
729: robj = KsocketClose(obj);
730: }else {
731: errorMsg1s("Unknown tag for sm1.socket");
732: }
733: return(robj);
734: }
735:
736:
1.8 takayama 737: static int getContentLength(char *s) {
738: int n;
1.10 takayama 739: int i,j;
1.8 takayama 740: int len = -1;
1.10 takayama 741: char *s1 = "content-length:";
742: char s0[256];
743: int m;
744: m = strlen(s1);
1.8 takayama 745: n = strlen(s);
746: for (i=0; i<n; i++) {
1.10 takayama 747: strncpy(s0,&(s[i]),m+1);
748: for (j=0; j<m; j++) {
749: if ((s0[j] >= 'A') && (s0[j] <= 'Z')) s0[j] = s0[j]+0x20;
750: }
751: if (strncmp(s0,s1,strlen(s1)) == 0) {
752: sscanf(&(s[i+strlen(s1)]),"%d",&len);
753: break;
754: }
1.8 takayama 755: }
756: return len;
757: }
758: static int getReceivedContentLength(char *s) {
759: int n;
760: int i;
761: int start;
762: start = -1;
763: n = strlen(s);
764: for (i=0; i<n; i++) {
765: if ((s[i] == '\n') && (s[i+1] == '\n')) {
766: start = i+2; break;
767: }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (s[i+2] == 0xd) && (s[i+3] == 0xa)) {
768: start = i+4;
769: }
770: }
771: if (start == -1) return 0;
772: return (n-start);
773: }
1.1 maekawa 774:
775:
1.11 takayama 776: struct object KsocketGetHostName(void) {
777: char name[1024];
778: char *s;
779: struct object rob = NullObject;
780: if (gethostname(name,1023) != 0) {
781: return rob;
782: }
783: s = (char *)GC_malloc(sizeof(char)*(strlen(name)+2));
784: if (s == (char *)NULL) errorMsg1s("Out of Memory.");
785: strcpy(s,name);
786: return(KpoString(s));
787: }
1.1 maekawa 788:
789:
790:
791:
792:
793:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>