=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/sm1Socket.c,v retrieving revision 1.8 retrieving revision 1.14 diff -u -p -r1.8 -r1.14 --- OpenXM/src/kan96xx/plugin/sm1Socket.c 2002/10/21 01:11:44 1.8 +++ OpenXM/src/kan96xx/plugin/sm1Socket.c 2002/10/30 13:23:06 1.14 @@ -1,4 +1,4 @@ -/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.7 2002/10/21 00:37:00 takayama Exp $ */ +/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.13 2002/10/24 02:12:35 takayama Exp $ */ /* msg0s.c */ #include #include @@ -14,6 +14,7 @@ #include "sm1Socket.h" extern int Quiet; +static int Post_debug=0; static void errorMsg1s(char *s); static int getContentLength(char *s); static int getReceivedContentLength(char *s); @@ -30,7 +31,12 @@ struct object KsocketOpen(struct object obj) { int on; int tt; extern int errno; + extern int Post_debug; + if ((char *)getenv("OXWEB_DEBUG") != NULL) { + Post_debug = 1; + } + if (obj.tag != Sarray) { errorMsg1s("KsocketOpen([optional integer,optional string name])"); } @@ -183,6 +189,42 @@ struct object KsocketAccept(struct object obj) { return(robj); } +/* [ integer socketid ] + [ integer newsocketid ] */ +/* It does not close the listening socket. You can call it as + ls = open. + fd=accept2(ls). close(fd). + fd=accept2(ls). close(fd). + .... + */ +struct object KsocketAccept2(struct object obj) { + struct object obj1; + struct object obj2; + struct object robj; + int s, news; + + if (obj.tag != Sarray) { + errorMsg1s("KsocketAccept([integer socketid])"); + } + if (getoaSize(obj) < 1) { + errorMsg1s("KsocketAccept([integer socketid])"); + } + obj1 = getoa(obj,0); + if (obj1.tag != Sinteger ) { + errorMsg1s("KsocketAccept([integer socketid]), argument must be integer."); + } + s = KopInteger(obj1); + if ((news = accept(s,NULL,NULL)) < 0) { + errorMsg1s("Error in accept."); + } + if (close(s) < 0) { + errorMsg1s("Error in closing the old socket."); + } + robj = newObjectArray(1); + putoa(robj,0,KpoInteger(news)); + return(robj); +} + int KsocketSelect0(int fd,int t) { fd_set readfds; struct timeval timeout; @@ -534,6 +576,8 @@ struct object KsocketWriteByte(struct object obj) { struct object KsocketReadHTTP(struct object socketObj) { /* Read until two empty line appears. */ struct object ob; + struct object ob1; + struct object nob; char *s; char *sss; char *tmp; @@ -543,6 +587,25 @@ struct object KsocketReadHTTP(struct object socketObj) int datasize; int last; int contentLength=-1; + int socketid; + extern int Post_debug; + nob = NullObject; + + if (socketObj.tag != Sarray) { + errorMsg1s("KsocketReadHTTP([integer socketid])"); + } + if (getoaSize(socketObj) < 1) { + errorMsg1s("KsocketReadHTTP([integer socketid])"); + } + ob1 = getoa(socketObj,0); + if (ob1.tag != Sinteger) { + errorMsg1s("KsocketReadHTTP([integer socketid]) : the argument must be integer."); + } + socketid = KopInteger(ob1); + + if (KsocketSelect0(socketid,-1) != 1) { + return(nob); + } ob = KsocketRead(socketObj); s = KopString(ob); if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */ @@ -574,22 +637,31 @@ struct object KsocketReadHTTP(struct object socketObj) break; } } - fprintf(stderr,"Waiting in socketReadBlock (spin lock to wait an empty line). flagmax(0d,0a)=%d, contentLength\n",flagmax,contentLength); - if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n"); + if (Post_debug) { + fprintf(stderr,"Waiting in socketReadBlock. flagmax(0d,0a)=%d, content-length=%d, received content-length=%d\n",flagmax,contentLength,getReceivedContentLength(sss)); + } + if (strlen(s) == 0) { + fprintf(stderr,"No data. Perhaps connection is closed by foreign host.\n"); + return nob; }else{ /* for debugging. */ - for (i=0; i= ' ') && (sss[i] < 0x7f)) { - fprintf(stderr,"%c",sss[i]); - }else{ - fprintf(stderr,"%3x",sss[i]); - if (sss[i] == 0xa) fprintf(stderr,"\n"); - } + if (Post_debug) { + for (i=0; i= ' ') && (sss[i] < 0x7f)) { + fprintf(stderr,"%c",sss[i]); + }else{ + fprintf(stderr,"(%3x)",sss[i]); + if (sss[i] == 0xa) fprintf(stderr,"\n"); + } + } + fprintf(stderr,"\n"); } - fprintf(stderr,"\n"); } - sleep(2); + + if (KsocketSelect0(socketid,-1) != 1) { + return nob; + } ob = KsocketRead(socketObj); s = KopString(ob); for (i=strlen(s)-1; i>=0; i--) { @@ -633,6 +705,8 @@ struct object Kplugin_sm1Socket(char *key,struct objec robj = KsocketConnect(obj); }else if (strcmp(key,"accept") == 0) { robj = KsocketAccept(obj); + }else if (strcmp(key,"accept2") == 0) { + robj = KsocketAccept2(obj); }else if (strcmp(key,"select") == 0) { robj = KsocketSelect(obj); }else if (strcmp(key,"mselect") == 0) { @@ -641,6 +715,8 @@ struct object Kplugin_sm1Socket(char *key,struct objec robj = KsocketRead(obj); }else if (strcmp(key,"readHTTP") == 0) { robj = KsocketReadHTTP(obj); + }else if (strcmp(key,"gethostname") == 0) { + robj = KsocketGetHostName(); }else if (strcmp(key,"write") == 0) { robj = KsocketWrite(obj); }else if (strcmp(key,"read") == 0) { @@ -660,17 +736,22 @@ struct object Kplugin_sm1Socket(char *key,struct objec static int getContentLength(char *s) { int n; - int i; + int i,j; int len = -1; - char *s1 = "Content-length:"; - char *s2 = "CONTENT-LENGTH:"; + char *s1 = "content-length:"; + char s0[256]; + int m; + m = strlen(s1); n = strlen(s); for (i=0; i= 'A') && (s0[j] <= 'Z')) s0[j] = s0[j]+0x20; + } + if (strncmp(s0,s1,strlen(s1)) == 0) { + sscanf(&(s[i+strlen(s1)]),"%d",&len); + break; + } } return len; } @@ -692,6 +773,18 @@ static int getReceivedContentLength(char *s) { } +struct object KsocketGetHostName(void) { + char name[1024]; + char *s; + struct object rob = NullObject; + if (gethostname(name,1023) != 0) { + return rob; + } + s = (char *)GC_malloc(sizeof(char)*(strlen(name)+2)); + if (s == (char *)NULL) errorMsg1s("Out of Memory."); + strcpy(s,name); + return(KpoString(s)); +}