version 1.1, 2014/08/30 12:25:56 |
version 1.4, 2014/08/31 10:20:33 |
|
|
/* $OpenXM$ |
/* $OpenXM: OpenXM/src/asir-port/cgi/webasir2.c,v 1.3 2014/08/31 07:53:57 takayama Exp $ |
*/ |
*/ |
/* |
/* |
(httpd-asir2.sm1) run webasir2 |
(httpd-asir2.sm1) run webasir2 |
|
>log 2>&1 |
|
Todo, timer(limit, command, message) implement in sm1. |
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
#include <sys/types.h> |
#include <sys/types.h> |
|
#include <unistd.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
#include <sys/time.h> |
#include <sys/time.h> |
#include <netinet/in.h> |
#include <netinet/in.h> |
|
|
#include <string.h> |
#include <string.h> |
|
|
#define SIZE 0x10000 |
#define SIZE 0x10000 |
|
#define MKEY "oxMessageBody=" |
|
char *byteArrayToUrlEncoding(char *s,int size); |
|
static int cgiHex(int p); |
|
char *urlEncodedStringToString(char *s); |
|
|
int Debug=1; |
int Debug=1; |
|
int SetTimer=0; |
int main(int argc,char *argv[]) { |
int main(int argc,char *argv[]) { |
int dataPort; |
int dataPort; |
struct hostent *servhost; |
struct hostent *servhost; |
Line 21 int main(int argc,char *argv[]) { |
|
Line 30 int main(int argc,char *argv[]) { |
|
FILE *fp; |
FILE *fp; |
char s[1]; |
char s[1]; |
char fname[SIZE]; |
char fname[SIZE]; |
int i; |
int i,j,c; |
char key[SIZE]; |
char key[SIZE]; |
char comm[SIZE]; |
char comm[SIZE]; |
|
char asircomm[SIZE]; |
int quit; |
int quit; |
|
char workf[SIZE]; |
quit = 0; |
quit = 0; |
|
strcpy(asircomm,"3-2;"); |
|
strcpy(asircomm,"oxMessageBody=1%2B3%3B"); |
for (i=1; i<argc; i++) { |
for (i=1; i<argc; i++) { |
if (strcmp(argv[i],"--quit")==0) quit=1; |
if (strcmp(argv[i],"--quit")==0) quit=1; |
|
else if (strcmp(argv[i],"--asir")==0) { |
|
i++; |
|
if (i <argc) strcpy(asircomm,argv[i]); |
|
else { usage(); return(-1); } |
|
}else if (strcmp(argv[i],"--debug")==0) { |
|
i++; |
|
if (i <argc) sscanf(argv[i],"%d",&Debug); |
|
else { usage(); return(-1); } |
|
}else if (strcmp(argv[i],"--settimer")==0) { |
|
i++; |
|
if (i <argc) sscanf(argv[i],"%d",&SetTimer); |
|
else { usage(); return(-1); } |
|
} else if (strcmp(argv[i],"--stdin")==0) { |
|
asircomm[0] = 0; j=0; |
|
while ((c=getchar()) != EOF) { |
|
asircomm[j] = c; j++; asircomm[j] = 0; |
|
if (j > SIZE-3) { |
|
fprintf(stderr,"error, too big input.\n"); return(-1); |
|
} |
|
} |
|
} else { |
|
usage(); return(0); |
|
} |
} |
} |
|
|
system("ls /tmp/webasir*.txt >tmp-webasir.txt"); |
sprintf(workf,"/tmp/tmp-webasir-%d.txt",(int) getpid()); |
fp = fopen("tmp-webasir.txt","r"); |
sprintf(comm,"ls /tmp/webasir*.txt >%s",workf); |
|
system(comm); |
|
fp = fopen(workf,"r"); |
if (fp == NULL) { |
if (fp == NULL) { |
fprintf(stderr,"No webasir2 is running.\n"); return(-1); |
fprintf(stderr,"No webasir2 is running.\n"); return(-1); |
} |
} |
Line 55 int main(int argc,char *argv[]) { |
|
Line 93 int main(int argc,char *argv[]) { |
|
} |
} |
if (Debug) printf("key=%s\n",key); |
if (Debug) printf("key=%s\n",key); |
fclose(fp); |
fclose(fp); |
|
sprintf(comm,"rm -f %s",workf); |
|
system(comm); |
|
|
if ((servhost = gethostbyname("localhost")) == NULL) { |
if ((servhost = gethostbyname("localhost")) == NULL) { |
fprintf(stderr,"bad server name.\n"); return(-1); |
fprintf(stderr,"bad server name.\n"); return(-1); |
Line 72 int main(int argc,char *argv[]) { |
|
Line 112 int main(int argc,char *argv[]) { |
|
if (Debug) fprintf(stderr,"Trying to connect port %d ",ntohs(dServer.sin_port)); |
if (Debug) fprintf(stderr,"Trying to connect port %d ",ntohs(dServer.sin_port)); |
|
|
if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) { |
if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) { |
fprintf(stderr,"cannot connect\n"); |
fprintf(stderr,"error: cannot connect\n"); |
}else{ fprintf(stderr,"Connected\n"); } |
}else{ if (Debug) fprintf(stderr,"Connected\n"); } |
|
|
|
/* If the input is MKEY=..., extract ... */ |
|
if (strncmp(asircomm,MKEY,strlen(MKEY))==0) { |
|
strcpy(comm,&(asircomm[strlen(MKEY)])); |
|
strcpy(asircomm,comm); |
|
strcpy(asircomm,urlEncodedStringToString(asircomm)); |
|
if (Debug) fprintf(stderr,"oxMessageBody, asircomm=%s\n",asircomm); |
|
} |
|
|
|
if (SetTimer) { |
|
strcpy(comm,asircomm); |
|
for (i=strlen(comm)-1; i>=0; i--) { |
|
if ((comm[i] == ';') || (comm[i] <= ' ')) comm[i] = 0; |
|
else break; |
|
} |
|
sprintf(asircomm,"timer(%d,%s,\"error(timeout %d sec)\");", |
|
SetTimer,comm,SetTimer); |
|
} |
if (quit) { |
if (quit) { |
sprintf(comm,"GET /?msg=httpdAsirMeta+quit HTTP/1.1\n\n"); |
sprintf(comm,"GET /?msg=httpdAsirMeta+quit HTTP/1.1\n\n"); |
write(dataPort,comm,strlen(comm)); |
write(dataPort,comm,strlen(comm)); |
fflush(NULL); |
fflush(NULL); |
}else{ |
}else{ |
sprintf(comm,"GET /?%s=10-21;\n\n",key); |
sprintf(comm,"GET /?%s=%s;\n\n",key,byteArrayToUrlEncoding(asircomm,strlen(asircomm))); |
write(dataPort,comm,strlen(comm)); |
write(dataPort,comm,strlen(comm)); |
fflush(NULL); |
fflush(NULL); |
} |
} |
Line 91 int main(int argc,char *argv[]) { |
|
Line 148 int main(int argc,char *argv[]) { |
|
printf("%s\n",comm); |
printf("%s\n",comm); |
} |
} |
|
|
|
/* from kan96xx/plugin/oxcgi.c */ |
|
/* . - _ A-Z a-z 0-9 |
|
space --> + |
|
*/ |
|
static int isUrlEncoding3(char s) { |
|
if ((s == '.') || (s == '-') || (s == '_')) return(0); |
|
if ((s >= 'A') && (s <= 'Z')) return(0); |
|
if ((s >= 'a') && (s <= 'z')) return(0); |
|
if ((s >= '0') && (s <= '9')) return(0); |
|
if (s == ' ') return(0); |
|
return(1); |
|
} |
|
|
|
char *byteArrayToUrlEncoding(char *s,int size) { |
|
int n,i,j; |
|
char *r; |
|
n = 0; |
|
/* get Size */ |
|
for (i=0; i<size; i++) { |
|
if (isUrlEncoding3((char)s[i])) n += 3; |
|
n++; |
|
} |
|
r = malloc(n+1); |
|
if (r == NULL) {fprintf(stderr,"%s\n","No more memory."); return(NULL); } |
|
r[0] = 0; r[n] = 0; |
|
i = 0; j = 0; |
|
while ((j < n) && (i<size)) { |
|
if (isUrlEncoding3((char)s[i])) { |
|
sprintf(&(r[j]),"%%%02X",s[i]); j += 3; |
|
}else{ |
|
if ((char)s[i] == ' ') r[j]='+'; |
|
else r[j] = s[i]; |
|
j++; r[j] = 0; |
|
} |
|
i++; |
|
} |
|
return(r); |
|
} |
|
|
|
|
|
char *urlEncodedStringToString(char *s) |
|
{ |
|
char *ts; |
|
char *ts2; |
|
int i,j; |
|
int p; |
|
int vstart, vend; |
|
if (s == NULL) return(NULL); |
|
vstart = 0; vend = strlen(s)-1; |
|
ts = (char *) malloc(strlen(s)+2); |
|
if (ts == NULL) ; |
|
j = 0; ts[j] = 0; |
|
for (i=vstart; i<=vend; i++,j++) { |
|
ts[j] = 0; |
|
if (s[i] == '+') { |
|
ts[j] = ' '; ts[j+1] = 0; |
|
}else if (s[i] == '%') { |
|
p = cgiHex(s[i+1])*16+cgiHex(s[i+2]); |
|
i = i+2; |
|
ts[j] = p; ts[j+1] = 0; |
|
}else { |
|
ts[j] = s[i]; ts[j+1] = 0; |
|
} |
|
} |
|
ts2 = (char *) malloc(j); |
|
if (ts2 == NULL) ; |
|
for (i=0; i<j; i++) { |
|
ts2[i] = ts[i]; ts2[i+1] = 0; |
|
} |
|
return (ts2); |
|
} |
|
|
|
static int cgiHex(int p) { |
|
if (p >= '0' && p <= '9') return (p-'0'); |
|
if (p >= 'A' && p <= 'F') return (p-'A'+10); |
|
if (p >= 'a' && p <= 'f') return (p-'a'+10); |
|
if (Debug) fprintf(stderr,"%s\n","Invalid argument to cgiHex."); |
|
} |
|
|
|
usage() { |
|
fprintf(stderr,"webasir2 [--quit] [--asir command_string]\n"); |
|
fprintf(stderr," [--debug level]\n"); |
|
fprintf(stderr," [--settimer seconds]\n"); |
|
fprintf(stderr,"webasir2 --stdin ; command is obtained from the stdin.\n"); |
|
} |