version 1.3, 2001/05/04 01:06:30 |
version 1.16, 2004/02/23 09:03:43 |
|
|
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.2 2000/01/16 07:55:48 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.15 2003/09/16 02:57:40 takayama Exp $ */ |
/* msg0s.c */ |
/* msg0s.c */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
|
|
#include "sm1Socket.h" |
#include "sm1Socket.h" |
|
|
extern int Quiet; |
extern int Quiet; |
|
static int Post_debug=0; |
static void errorMsg1s(char *s); |
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 ; */ |
/* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */ |
struct object KsocketOpen(struct object obj) { |
struct object KsocketOpen(struct object obj) { |
Line 27 struct object KsocketOpen(struct object obj) { |
|
Line 31 struct object KsocketOpen(struct object obj) { |
|
int on; |
int on; |
int tt; |
int tt; |
extern int errno; |
extern int errno; |
|
extern int Post_debug; |
|
|
|
if ((char *)getenv("OXWEB_DEBUG") != NULL) { |
|
Post_debug = 1; |
|
} |
|
|
if (obj.tag != Sarray) { |
if (obj.tag != Sarray) { |
errorMsg1s("KsocketOpen([optional integer,optional string name])"); |
errorMsg1s("KsocketOpen([optional integer,optional string name])"); |
} |
} |
Line 78 struct object KsocketOpen(struct object obj) { |
|
Line 87 struct object KsocketOpen(struct object obj) { |
|
} |
} |
|
|
|
|
if (listen(s_waiting,1) < 0) { |
if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) { |
errorMsg1s("Listen failed"); |
errorMsg1s("Listen failed"); |
} |
} |
/* |
/* |
Line 170 struct object KsocketAccept(struct object obj) { |
|
Line 179 struct object KsocketAccept(struct object obj) { |
|
} |
} |
s = KopInteger(obj1); |
s = KopInteger(obj1); |
if ((news = accept(s,NULL,NULL)) < 0) { |
if ((news = accept(s,NULL,NULL)) < 0) { |
errorMsg1s("Error in accept."); |
fprintf(stderr,"Error in accept. Retrying (KsocketAccept).\n"); |
|
/* Code added for strange behavior on cygwin. */ |
|
if ((news = accept(s,NULL,NULL)) < 0) { |
|
errorMsg1s("Error in accept. Retry failed."); |
|
} |
} |
} |
if (close(s) < 0) { |
if (close(s) < 0) { |
errorMsg1s("Error in closing the old socket."); |
errorMsg1s("Error in closing the old socket."); |
Line 180 struct object KsocketAccept(struct object obj) { |
|
Line 193 struct object KsocketAccept(struct object obj) { |
|
return(robj); |
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) { |
|
fprintf(stderr,"Error in accept. Retrying (KsocketAccept2).\n"); |
|
/* Code added for strange behavior on cygwin. */ |
|
if ((news = accept(s,NULL,NULL)) < 0) { |
|
errorMsg1s("Error in accept. Retry failed."); |
|
} |
|
} |
|
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) { |
int KsocketSelect0(int fd,int t) { |
fd_set readfds; |
fd_set readfds; |
struct timeval timeout; |
struct timeval timeout; |
Line 360 struct object KsocketRead(struct object obj) { |
|
Line 413 struct object KsocketRead(struct object obj) { |
|
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 */ |
}else { /* increase the datasize */ |
tmp = (char *)GC_malloc(sizeof(char)*2*datasize); |
tmp = (char *)sGC_malloc(sizeof(char)*2*datasize); |
/*I should use GC_malloc_atomic and free after finishing this function?*/ |
/*I should use sGC_malloc_atomic and free after finishing this function?*/ |
if (tmp == (char *)NULL) errorMsg1s("Out of Memory."); |
if (tmp == (char *)NULL) errorMsg1s("Out of Memory."); |
bcopy(data,tmp,totalsize); |
bcopy(data,tmp,totalsize); |
data = tmp; |
data = tmp; |
Line 369 struct object KsocketRead(struct object obj) { |
|
Line 422 struct object KsocketRead(struct object obj) { |
|
} |
} |
} |
} |
|
|
r = (char *)GC_malloc(sizeof(char)*(n+1)); |
r = (char *)sGC_malloc(sizeof(char)*(totalsize+1)); |
if (r == (char *)NULL) errorMsg1s("Out of Memory."); |
if (r == (char *)NULL) errorMsg1s("Out of Memory."); |
bcopy(data,r,totalsize); |
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); |
return(robj); |
|
|
Line 476 struct object KsocketWriteByte(struct object obj) { |
|
Line 531 struct object KsocketWriteByte(struct object obj) { |
|
struct object ob1; |
struct object ob1; |
struct object ob2; |
struct object ob2; |
int socketid; |
int socketid; |
int r; |
int r,i,n,kk,r0; |
char data[2]; |
#define DATA_SIZE 1024 |
|
char data[DATA_SIZE]; |
if (obj.tag != Sarray) { |
if (obj.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
if (getoaSize(obj) < 2) { |
if (getoaSize(obj) < 2) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
ob1 = getoa(obj,0); |
ob1 = getoa(obj,0); |
ob2 = getoa(obj,1); |
ob2 = getoa(obj,1); |
if (ob1.tag != Sinteger) { |
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); |
socketid = KopInteger(ob1); |
if (ob2.tag != Sinteger) { |
if (ob2.tag != Sinteger && ob2.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int]) : the second argument must be a string."); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers."); |
} |
} |
data[0] = KopInteger(ob2); |
if (ob2.tag == Sinteger) { |
r = write(socketid,data, 1); |
data[0] = KopInteger(ob2); |
|
r = write(socketid,data, 1); |
|
}else{ |
|
n = getoaSize(ob2); kk = 0; r = 0; |
|
for (i=0; i<n; i++) { |
|
if (getoa(ob2,i).tag != Sinteger) |
|
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers."); |
|
data[kk] = KopInteger(getoa(ob2,i)); |
|
kk++; |
|
if (kk >= 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)); |
return(KpoInteger(r)); |
} |
} |
|
|
|
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; |
|
int i; |
|
int flag; |
|
int flagmax = 1; |
|
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 */ |
|
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; |
|
} |
|
} |
|
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. */ |
|
if (Post_debug) { |
|
for (i=0; i<strlen(sss); i++) { |
|
if ((sss[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"); |
|
} |
|
} |
|
|
|
if (KsocketSelect0(socketid,-1) != 1) { |
|
return nob; |
|
} |
|
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 *)sGC_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 Kplugin_sm1Socket(char *key,struct object obj) { |
struct object robj = NullObject; |
struct object robj = NullObject; |
if (strcmp(key,"open") == 0) { |
if (strcmp(key,"open") == 0) { |
Line 506 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 713 struct object Kplugin_sm1Socket(char *key,struct objec |
|
robj = KsocketConnect(obj); |
robj = KsocketConnect(obj); |
}else if (strcmp(key,"accept") == 0) { |
}else if (strcmp(key,"accept") == 0) { |
robj = KsocketAccept(obj); |
robj = KsocketAccept(obj); |
|
}else if (strcmp(key,"accept2") == 0) { |
|
robj = KsocketAccept2(obj); |
}else if (strcmp(key,"select") == 0) { |
}else if (strcmp(key,"select") == 0) { |
robj = KsocketSelect(obj); |
robj = KsocketSelect(obj); |
}else if (strcmp(key,"mselect") == 0) { |
}else if (strcmp(key,"mselect") == 0) { |
robj = KsocketSelectMulti(obj); |
robj = KsocketSelectMulti(obj); |
}else if (strcmp(key,"read") == 0) { |
}else if (strcmp(key,"read") == 0) { |
robj = KsocketRead(obj); |
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) { |
}else if (strcmp(key,"write") == 0) { |
robj = KsocketWrite(obj); |
robj = KsocketWrite(obj); |
}else if (strcmp(key,"read") == 0) { |
}else if (strcmp(key,"read") == 0) { |
Line 529 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 742 struct object Kplugin_sm1Socket(char *key,struct objec |
|
} |
} |
|
|
|
|
|
static int getContentLength(char *s) { |
|
int n; |
|
int i,j; |
|
int len = -1; |
|
char *s1 = "content-length:"; |
|
char s0[256]; |
|
int m; |
|
m = strlen(s1); |
|
n = strlen(s); |
|
for (i=0; i<n; i++) { |
|
strncpy(s0,&(s[i]),m+1); |
|
for (j=0; j<m; j++) { |
|
if ((s0[j] >= '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; |
|
} |
|
static int getReceivedContentLength(char *s) { |
|
int n; |
|
int i; |
|
int start; |
|
start = -1; |
|
n = strlen(s); |
|
for (i=0; i<n; i++) { |
|
if ((s[i] == '\n') && (s[i+1] == '\n')) { |
|
start = i+2; break; |
|
}else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (s[i+2] == 0xd) && (s[i+3] == 0xa)) { |
|
start = i+4; |
|
} |
|
} |
|
if (start == -1) return 0; |
|
return (n-start); |
|
} |
|
|
|
|
|
struct object KsocketGetHostName(void) { |
|
char name[1024]; |
|
char *s; |
|
struct object rob = NullObject; |
|
if (gethostname(name,1023) != 0) { |
|
return rob; |
|
} |
|
s = (char *)sGC_malloc(sizeof(char)*(strlen(name)+2)); |
|
if (s == (char *)NULL) errorMsg1s("Out of Memory."); |
|
strcpy(s,name); |
|
return(KpoString(s)); |
|
} |
|
|
|
|
|
|