version 1.7, 2002/10/21 00:37:00 |
version 1.12, 2002/10/24 01:29:00 |
|
|
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.6 2002/10/20 07:58:18 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.11 2002/10/24 01:05:05 takayama Exp $ */ |
/* msg0s.c */ |
/* msg0s.c */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
|
|
|
|
extern int Quiet; |
extern int Quiet; |
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 |
#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 ; */ |
Line 540 struct object KsocketReadHTTP(struct object socketObj) |
|
Line 542 struct object KsocketReadHTTP(struct object socketObj) |
|
int flagmax = 1; |
int flagmax = 1; |
int datasize; |
int datasize; |
int last; |
int last; |
|
int contentLength=-1; |
ob = KsocketRead(socketObj); |
ob = KsocketRead(socketObj); |
s = KopString(ob); |
s = KopString(ob); |
if (strncmp(s,"POST",4) == 0) flagmax=2; |
if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */ |
else flagmax=1; |
else flagmax=1; |
flag = 0; |
flag = 0; |
for (i=strlen(s)-1; i>=0; i--) { |
for (i=strlen(s)-1; i>=0; i--) { |
Line 565 struct object KsocketReadHTTP(struct object socketObj) |
|
Line 568 struct object KsocketReadHTTP(struct object socketObj) |
|
}else last = 0; |
}else last = 0; |
|
|
while (flag < flagmax) { |
while (flag < flagmax) { |
fprintf(stderr,"Waiting in socketReadBlock (spin lock to wait an empty line). flagmax(0d,0a)=%d\n",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, content-length=%d, received content-length=%d\n",flagmax,contentLength,getReceivedContentLength(sss)); |
if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n"); |
if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n"); |
}else{ |
}else{ |
/* for debugging. */ |
/* for debugging. */ |
for (i=0; i<strlen(sss); i++) { |
for (i=0; i<strlen(sss); i++) { |
fprintf(stderr,"%3x",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"); |
fprintf(stderr,"\n"); |
} |
} |
Line 626 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 641 struct object Kplugin_sm1Socket(char *key,struct objec |
|
robj = KsocketRead(obj); |
robj = KsocketRead(obj); |
}else if (strcmp(key,"readHTTP") == 0) { |
}else if (strcmp(key,"readHTTP") == 0) { |
robj = KsocketReadHTTP(obj); |
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 643 struct object Kplugin_sm1Socket(char *key,struct objec |
|
Line 660 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 *)GC_malloc(sizeof(char)*(strlen(name)+2)); |
|
if (s == (char *)NULL) errorMsg1s("Out of Memory."); |
|
strcpy(s,name); |
|
return(KpoString(s)); |
|
} |
|
|
|
|
|
|