Annotation of OpenXM/src/asir-port/cgi/webasir2.c, Revision 1.4
1.4 ! takayama 1: /* $OpenXM: OpenXM/src/asir-port/cgi/webasir2.c,v 1.3 2014/08/31 07:53:57 takayama Exp $
1.1 takayama 2: */
3: /*
4: (httpd-asir2.sm1) run webasir2
1.3 takayama 5: >log 2>&1
6: Todo, timer(limit, command, message) implement in sm1.
1.1 takayama 7: */
8: #include <stdio.h>
1.2 takayama 9: #include <stdlib.h>
1.1 takayama 10: #include <sys/types.h>
1.3 takayama 11: #include <unistd.h>
1.1 takayama 12: #include <sys/socket.h>
13: #include <sys/time.h>
14: #include <netinet/in.h>
15: #include <netdb.h>
16: #include <string.h>
17:
18: #define SIZE 0x10000
1.4 ! takayama 19: #define MKEY "oxMessageBody="
1.2 takayama 20: char *byteArrayToUrlEncoding(char *s,int size);
1.4 ! takayama 21: static int cgiHex(int p);
! 22: char *urlEncodedStringToString(char *s);
1.1 takayama 23:
24: int Debug=1;
1.3 takayama 25: int SetTimer=0;
1.1 takayama 26: int main(int argc,char *argv[]) {
27: int dataPort;
28: struct hostent *servhost;
29: struct sockaddr_in dServer;
30: FILE *fp;
31: char s[1];
32: char fname[SIZE];
1.3 takayama 33: int i,j,c;
1.1 takayama 34: char key[SIZE];
35: char comm[SIZE];
1.4 ! takayama 36: char asircomm[SIZE];
1.1 takayama 37: int quit;
1.3 takayama 38: char workf[SIZE];
1.1 takayama 39: quit = 0;
1.4 ! takayama 40: strcpy(asircomm,"3-2;");
! 41: strcpy(asircomm,"oxMessageBody=1%2B3%3B");
1.1 takayama 42: for (i=1; i<argc; i++) {
43: if (strcmp(argv[i],"--quit")==0) quit=1;
1.2 takayama 44: else if (strcmp(argv[i],"--asir")==0) {
45: i++;
1.4 ! takayama 46: if (i <argc) strcpy(asircomm,argv[i]);
1.2 takayama 47: else { usage(); return(-1); }
1.3 takayama 48: }else if (strcmp(argv[i],"--debug")==0) {
49: i++;
50: if (i <argc) sscanf(argv[i],"%d",&Debug);
51: else { usage(); return(-1); }
52: }else if (strcmp(argv[i],"--settimer")==0) {
53: i++;
54: if (i <argc) sscanf(argv[i],"%d",&SetTimer);
55: else { usage(); return(-1); }
56: } else if (strcmp(argv[i],"--stdin")==0) {
57: asircomm[0] = 0; j=0;
58: while ((c=getchar()) != EOF) {
59: asircomm[j] = c; j++; asircomm[j] = 0;
60: if (j > SIZE-3) {
61: fprintf(stderr,"error, too big input.\n"); return(-1);
62: }
63: }
64: } else {
65: usage(); return(0);
1.2 takayama 66: }
1.1 takayama 67: }
68:
1.3 takayama 69: sprintf(workf,"/tmp/tmp-webasir-%d.txt",(int) getpid());
70: sprintf(comm,"ls /tmp/webasir*.txt >%s",workf);
71: system(comm);
72: fp = fopen(workf,"r");
1.1 takayama 73: if (fp == NULL) {
74: fprintf(stderr,"No webasir2 is running.\n"); return(-1);
75: }
76: fgets(fname,SIZE-2,fp);
77: for (i=strlen(fname)-1; i>=0; i--) {
78: if (fname[i] <= ' ') fname[i]=0; else break;
79: }
80: fclose(fp);
81: if (strlen(fname)==0) {
82: fprintf(stderr,"No webasir2 pid file.\n"); return(-1);
83: }
84: fp = fopen(fname,"r");
85: if (fp == NULL) {
86: fprintf(stderr,"Open error of %s\n",fname); return(-1);
87: }
88: fgets(key,SIZE-2,fp); sscanf(key,"%d",&dataPort);
89: if (Debug) printf("dataPort=%d\n",dataPort);
90: fgets(key,SIZE-2,fp);
91: for (i=strlen(key)-1; i>=0; i--) {
92: if (key[i] <= ' ') key[i]=0; else break;
93: }
94: if (Debug) printf("key=%s\n",key);
95: fclose(fp);
1.3 takayama 96: sprintf(comm,"rm -f %s",workf);
97: system(comm);
1.1 takayama 98:
99: if ((servhost = gethostbyname("localhost")) == NULL) {
100: fprintf(stderr,"bad server name.\n"); return(-1);
101: }
102:
103: /* Connecting to the data port */
104: bzero((char *)&dServer,sizeof(dServer));
105: dServer.sin_family = AF_INET;
106: dServer.sin_port = htons(dataPort);
107: bcopy(servhost->h_addr,
108: (char *)&dServer.sin_addr,servhost->h_length);
109: if ((dataPort = socket(AF_INET,SOCK_STREAM,0)) <0) {
110: fprintf(stderr,"socket allocation is failed.\n");
111: }
112: if (Debug) fprintf(stderr,"Trying to connect port %d ",ntohs(dServer.sin_port));
113:
114: if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) {
1.3 takayama 115: fprintf(stderr,"error: cannot connect\n");
116: }else{ if (Debug) fprintf(stderr,"Connected\n"); }
1.1 takayama 117:
1.4 ! takayama 118: /* If the input is MKEY=..., extract ... */
! 119: if (strncmp(asircomm,MKEY,strlen(MKEY))==0) {
! 120: strcpy(comm,&(asircomm[strlen(MKEY)]));
! 121: strcpy(asircomm,comm);
! 122: strcpy(asircomm,urlEncodedStringToString(asircomm));
! 123: if (Debug) fprintf(stderr,"oxMessageBody, asircomm=%s\n",asircomm);
! 124: }
! 125:
1.3 takayama 126: if (SetTimer) {
127: strcpy(comm,asircomm);
128: for (i=strlen(comm)-1; i>=0; i--) {
129: if ((comm[i] == ';') || (comm[i] <= ' ')) comm[i] = 0;
130: else break;
131: }
132: sprintf(asircomm,"timer(%d,%s,\"error(timeout %d sec)\");",
133: SetTimer,comm,SetTimer);
134: }
1.1 takayama 135: if (quit) {
136: sprintf(comm,"GET /?msg=httpdAsirMeta+quit HTTP/1.1\n\n");
137: write(dataPort,comm,strlen(comm));
138: fflush(NULL);
139: }else{
1.2 takayama 140: sprintf(comm,"GET /?%s=%s;\n\n",key,byteArrayToUrlEncoding(asircomm,strlen(asircomm)));
1.1 takayama 141: write(dataPort,comm,strlen(comm));
142: fflush(NULL);
143: }
144:
145: /* get result */
146: for (i=0; i<SIZE; i++) comm[i]=0;
147: read(dataPort,comm,SIZE-2);
148: printf("%s\n",comm);
149: }
150:
1.2 takayama 151: /* from kan96xx/plugin/oxcgi.c */
152: /* . - _ A-Z a-z 0-9
153: space --> +
154: */
155: static int isUrlEncoding3(char s) {
156: if ((s == '.') || (s == '-') || (s == '_')) return(0);
157: if ((s >= 'A') && (s <= 'Z')) return(0);
158: if ((s >= 'a') && (s <= 'z')) return(0);
159: if ((s >= '0') && (s <= '9')) return(0);
160: if (s == ' ') return(0);
161: return(1);
162: }
163:
164: char *byteArrayToUrlEncoding(char *s,int size) {
165: int n,i,j;
166: char *r;
167: n = 0;
168: /* get Size */
169: for (i=0; i<size; i++) {
170: if (isUrlEncoding3((char)s[i])) n += 3;
171: n++;
172: }
173: r = malloc(n+1);
174: if (r == NULL) {fprintf(stderr,"%s\n","No more memory."); return(NULL); }
175: r[0] = 0; r[n] = 0;
176: i = 0; j = 0;
177: while ((j < n) && (i<size)) {
178: if (isUrlEncoding3((char)s[i])) {
179: sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
180: }else{
181: if ((char)s[i] == ' ') r[j]='+';
182: else r[j] = s[i];
183: j++; r[j] = 0;
184: }
185: i++;
186: }
187: return(r);
188: }
189:
190:
1.4 ! takayama 191: char *urlEncodedStringToString(char *s)
! 192: {
! 193: char *ts;
! 194: char *ts2;
! 195: int i,j;
! 196: int p;
! 197: int vstart, vend;
! 198: if (s == NULL) return(NULL);
! 199: vstart = 0; vend = strlen(s)-1;
! 200: ts = (char *) malloc(strlen(s)+2);
! 201: if (ts == NULL) ;
! 202: j = 0; ts[j] = 0;
! 203: for (i=vstart; i<=vend; i++,j++) {
! 204: ts[j] = 0;
! 205: if (s[i] == '+') {
! 206: ts[j] = ' '; ts[j+1] = 0;
! 207: }else if (s[i] == '%') {
! 208: p = cgiHex(s[i+1])*16+cgiHex(s[i+2]);
! 209: i = i+2;
! 210: ts[j] = p; ts[j+1] = 0;
! 211: }else {
! 212: ts[j] = s[i]; ts[j+1] = 0;
! 213: }
! 214: }
! 215: ts2 = (char *) malloc(j);
! 216: if (ts2 == NULL) ;
! 217: for (i=0; i<j; i++) {
! 218: ts2[i] = ts[i]; ts2[i+1] = 0;
! 219: }
! 220: return (ts2);
! 221: }
! 222:
! 223: static int cgiHex(int p) {
! 224: if (p >= '0' && p <= '9') return (p-'0');
! 225: if (p >= 'A' && p <= 'F') return (p-'A'+10);
! 226: if (p >= 'a' && p <= 'f') return (p-'a'+10);
! 227: if (Debug) fprintf(stderr,"%s\n","Invalid argument to cgiHex.");
! 228: }
! 229:
1.2 takayama 230: usage() {
231: fprintf(stderr,"webasir2 [--quit] [--asir command_string]\n");
1.3 takayama 232: fprintf(stderr," [--debug level]\n");
233: fprintf(stderr," [--settimer seconds]\n");
234: fprintf(stderr,"webasir2 --stdin ; command is obtained from the stdin.\n");
1.2 takayama 235: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>