=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/sm1Socket.c,v retrieving revision 1.1.1.1 retrieving revision 1.8 diff -u -p -r1.1.1.1 -r1.8 --- OpenXM/src/kan96xx/plugin/sm1Socket.c 1999/10/08 02:12:05 1.1.1.1 +++ OpenXM/src/kan96xx/plugin/sm1Socket.c 2002/10/21 01:11:44 1.8 @@ -1,3 +1,4 @@ +/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.7 2002/10/21 00:37:00 takayama Exp $ */ /* msg0s.c */ #include #include @@ -14,6 +15,9 @@ extern int Quiet; static void errorMsg1s(char *s); +static int getContentLength(char *s); +static int getReceivedContentLength(char *s); +#define MAX_LISTEN_QUEUE 3 /* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */ struct object KsocketOpen(struct object obj) { @@ -57,7 +61,7 @@ struct object KsocketOpen(struct object obj) { me.sin_family = AF_INET; me.sin_port = htons(portNumber); bcopy(myhost->h_addr, - &me.sin_addr,myhost->h_length); + &me.sin_addr,myhost->h_length); if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) { errorMsg1s("Socket allocation is failed."); @@ -77,12 +81,12 @@ struct object KsocketOpen(struct object obj) { } - if (listen(s_waiting,1) < 0) { + if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) { errorMsg1s("Listen failed"); } /* - fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port)); - fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */ + fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port)); + fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */ rob = newObjectArray(2); putoa(rob,0,KpoInteger(s_waiting)); putoa(rob,1,KpoInteger(ntohs(me.sin_port))); @@ -131,7 +135,7 @@ struct object KsocketConnect(struct object obj) { server.sin_family = AF_INET; server.sin_port = htons(portNumber); bcopy(servhost->h_addr, - (char *)&server.sin_addr,servhost->h_length); + (char *)&server.sin_addr,servhost->h_length); if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) { errorMsg1s("socket allocation is failed.\n"); @@ -236,7 +240,7 @@ struct object KsocketSelect(struct object obj) { } struct object KsocketSelectMulti(struct object obj) -/* [ [integer socketid1, integer socketid2, ...] + /* [ [integer socketid1, integer socketid2, ...] optional integer timeout default 0] [ result1, result2, ....] */ @@ -349,14 +353,14 @@ struct object KsocketRead(struct object obj) { if (datasize - totalsize > 0) { n = read(socketid,data+totalsize,datasize-totalsize); if (n < 0) { - errorMsg1s("Read error."); + errorMsg1s("Read error."); } if (n < datasize-totalsize) { - totalsize += n; - break; + totalsize += n; + break; }else{ totalsize += n; } if (totalsize == 0) { - errorMsg1s("Select returns 1, but there is no data to read."); + errorMsg1s("Select returns 1, but there is no data to read."); } }else { /* increase the datasize */ tmp = (char *)GC_malloc(sizeof(char)*2*datasize); @@ -368,10 +372,12 @@ struct object KsocketRead(struct object obj) { } } - r = (char *)GC_malloc(sizeof(char)*(n+1)); + r = (char *)GC_malloc(sizeof(char)*(totalsize+1)); if (r == (char *)NULL) errorMsg1s("Out of Memory."); bcopy(data,r,totalsize); - robj = KpoString(r); + r[totalsize] = 0; + robj = KpoString(r); /* BUG: it works only for reading string from TCP/IP + stream. */ return(robj); @@ -475,28 +481,150 @@ struct object KsocketWriteByte(struct object obj) { struct object ob1; struct object ob2; int socketid; - int r; - char data[2]; + int r,i,n,kk,r0; +#define DATA_SIZE 1024 + char data[DATA_SIZE]; if (obj.tag != Sarray) { - errorMsg1s("KsocketWriteByte([integer socketid, int])"); + errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); } if (getoaSize(obj) < 2) { - errorMsg1s("KsocketWriteByte([integer socketid, int])"); + errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); } ob1 = getoa(obj,0); ob2 = getoa(obj,1); if (ob1.tag != Sinteger) { - errorMsg1s("KsocketWriteByte([integer socketid, int]) : the first argument must be an integer."); + errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer."); } socketid = KopInteger(ob1); - if (ob2.tag != Sinteger) { - errorMsg1s("KsocketWriteByte([integer socketid, int]) : the second argument must be a string."); + if (ob2.tag != Sinteger && ob2.tag != Sarray) { + errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers."); } - data[0] = KopInteger(ob2); - r = write(socketid,data, 1); + if (ob2.tag == Sinteger) { + data[0] = KopInteger(ob2); + r = write(socketid,data, 1); + }else{ + n = getoaSize(ob2); kk = 0; r = 0; + for (i=0; i= DATA_SIZE) { + r0 = write(socketid,data,kk); + if (r0 != kk) { + fprintf(stderr,"Warning: Could not write to the socket.\n"); + return(KpoInteger(r+r0)); + } + r += r0; + kk = 0; + } + } + if (kk > 0) { + r0 = write(socketid,data,kk); + if (r0 != kk) { + fprintf(stderr,"Warning: Could not write to the socket.\n"); + return(KpoInteger(r+r0)); + } + r += r0; + } + } return(KpoInteger(r)); } +struct object KsocketReadHTTP(struct object socketObj) { + /* Read until two empty line appears. */ + struct object ob; + char *s; + char *sss; + char *tmp; + int i; + int flag; + int flagmax = 1; + int datasize; + int last; + int contentLength=-1; + ob = KsocketRead(socketObj); + s = KopString(ob); + if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */ + else flagmax=1; + flag = 0; + for (i=strlen(s)-1; i>=0; i--) { + if ((s[i] == '\n') && (i==0)) { + ++flag; + }else if ((s[i] == '\n') && (s[i-1] == '\n')) { + ++flag; + }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) { + ++flag; + }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) { + ++flag; + } + } + if (flag >= flagmax) return ob; + datasize = strlen(s); + sss = s; + if ((s[strlen(s)-1] == '\n') || + (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) { + last = 1; + }else last = 0; + + while (flag < flagmax) { + contentLength = getContentLength(sss); + if (contentLength != -1) { + if (contentLength <= getReceivedContentLength(sss)) { + 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"); + }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"); + } + + } + fprintf(stderr,"\n"); + } + sleep(2); + ob = KsocketRead(socketObj); + s = KopString(ob); + for (i=strlen(s)-1; i>=0; i--) { + if ((s[i] == '\n') && (i==0) && last) { + ++flag; + }else if ((s[i] == '\n') && (s[i-1] == '\n')) { + ++flag; + }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i==0) && last) { + ++flag; + }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) { + ++flag; + } + } + if (datasize-1 <= strlen(sss)+strlen(s)) { + tmp = (char *)GC_malloc(sizeof(char)*2*(datasize+strlen(s))+1); + if (tmp == (char *)NULL) errorMsg1s("Out of Memory."); + strcpy(tmp,sss); + strcat(tmp,s); + datasize = 2*(datasize+strlen(s)); + sss = tmp; + }else{ + strcat(sss,s); + } + + if ((s[strlen(s)-1] == '\n') || + (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) { + last = 1; + }else last = 0; + + } + + return KpoString(sss); + +} + struct object Kplugin_sm1Socket(char *key,struct object obj) { struct object robj = NullObject; if (strcmp(key,"open") == 0) { @@ -511,6 +639,8 @@ struct object Kplugin_sm1Socket(char *key,struct objec robj = KsocketSelectMulti(obj); }else if (strcmp(key,"read") == 0) { robj = KsocketRead(obj); + }else if (strcmp(key,"readHTTP") == 0) { + robj = KsocketReadHTTP(obj); }else if (strcmp(key,"write") == 0) { robj = KsocketWrite(obj); }else if (strcmp(key,"read") == 0) { @@ -528,6 +658,38 @@ struct object Kplugin_sm1Socket(char *key,struct objec } +static int getContentLength(char *s) { + int n; + int i; + int len = -1; + char *s1 = "Content-length:"; + char *s2 = "CONTENT-LENGTH:"; + n = strlen(s); + for (i=0; i