Annotation of OpenXM/src/asir-port/cgi/webasir2.c, Revision 1.3
1.3 ! takayama 1: /* $OpenXM: OpenXM/src/asir-port/cgi/webasir2.c,v 1.2 2014/08/30 22:47:20 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.2 takayama 19: char *byteArrayToUrlEncoding(char *s,int size);
1.1 takayama 20:
21: int Debug=1;
1.3 ! takayama 22: int SetTimer=0;
1.1 takayama 23: int main(int argc,char *argv[]) {
24: int dataPort;
25: struct hostent *servhost;
26: struct sockaddr_in dServer;
27: FILE *fp;
28: char s[1];
29: char fname[SIZE];
1.3 ! takayama 30: int i,j,c;
1.1 takayama 31: char key[SIZE];
32: char comm[SIZE];
1.2 takayama 33: char *asircomm;
1.1 takayama 34: int quit;
1.3 ! takayama 35: char workf[SIZE];
1.1 takayama 36: quit = 0;
1.2 takayama 37: asircomm="3-2;";
1.1 takayama 38: for (i=1; i<argc; i++) {
39: if (strcmp(argv[i],"--quit")==0) quit=1;
1.2 takayama 40: else if (strcmp(argv[i],"--asir")==0) {
41: i++;
42: if (i <argc) asircomm = argv[i];
43: else { usage(); return(-1); }
1.3 ! takayama 44: }else if (strcmp(argv[i],"--debug")==0) {
! 45: i++;
! 46: if (i <argc) sscanf(argv[i],"%d",&Debug);
! 47: else { usage(); return(-1); }
! 48: }else if (strcmp(argv[i],"--settimer")==0) {
! 49: i++;
! 50: if (i <argc) sscanf(argv[i],"%d",&SetTimer);
! 51: else { usage(); return(-1); }
! 52: } else if (strcmp(argv[i],"--stdin")==0) {
! 53: asircomm = (char *) malloc(SIZE);
! 54: asircomm[0] = 0; j=0;
! 55: while ((c=getchar()) != EOF) {
! 56: asircomm[j] = c; j++; asircomm[j] = 0;
! 57: if (j > SIZE-3) {
! 58: fprintf(stderr,"error, too big input.\n"); return(-1);
! 59: }
! 60: }
! 61: } else {
! 62: usage(); return(0);
1.2 takayama 63: }
1.1 takayama 64: }
65:
1.3 ! takayama 66: sprintf(workf,"/tmp/tmp-webasir-%d.txt",(int) getpid());
! 67: sprintf(comm,"ls /tmp/webasir*.txt >%s",workf);
! 68: system(comm);
! 69: fp = fopen(workf,"r");
1.1 takayama 70: if (fp == NULL) {
71: fprintf(stderr,"No webasir2 is running.\n"); return(-1);
72: }
73: fgets(fname,SIZE-2,fp);
74: for (i=strlen(fname)-1; i>=0; i--) {
75: if (fname[i] <= ' ') fname[i]=0; else break;
76: }
77: fclose(fp);
78: if (strlen(fname)==0) {
79: fprintf(stderr,"No webasir2 pid file.\n"); return(-1);
80: }
81: fp = fopen(fname,"r");
82: if (fp == NULL) {
83: fprintf(stderr,"Open error of %s\n",fname); return(-1);
84: }
85: fgets(key,SIZE-2,fp); sscanf(key,"%d",&dataPort);
86: if (Debug) printf("dataPort=%d\n",dataPort);
87: fgets(key,SIZE-2,fp);
88: for (i=strlen(key)-1; i>=0; i--) {
89: if (key[i] <= ' ') key[i]=0; else break;
90: }
91: if (Debug) printf("key=%s\n",key);
92: fclose(fp);
1.3 ! takayama 93: sprintf(comm,"rm -f %s",workf);
! 94: system(comm);
1.1 takayama 95:
96: if ((servhost = gethostbyname("localhost")) == NULL) {
97: fprintf(stderr,"bad server name.\n"); return(-1);
98: }
99:
100: /* Connecting to the data port */
101: bzero((char *)&dServer,sizeof(dServer));
102: dServer.sin_family = AF_INET;
103: dServer.sin_port = htons(dataPort);
104: bcopy(servhost->h_addr,
105: (char *)&dServer.sin_addr,servhost->h_length);
106: if ((dataPort = socket(AF_INET,SOCK_STREAM,0)) <0) {
107: fprintf(stderr,"socket allocation is failed.\n");
108: }
109: if (Debug) fprintf(stderr,"Trying to connect port %d ",ntohs(dServer.sin_port));
110:
111: if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) {
1.3 ! takayama 112: fprintf(stderr,"error: cannot connect\n");
! 113: }else{ if (Debug) fprintf(stderr,"Connected\n"); }
1.1 takayama 114:
1.3 ! takayama 115: if (SetTimer) {
! 116: strcpy(comm,asircomm);
! 117: for (i=strlen(comm)-1; i>=0; i--) {
! 118: if ((comm[i] == ';') || (comm[i] <= ' ')) comm[i] = 0;
! 119: else break;
! 120: }
! 121: sprintf(asircomm,"timer(%d,%s,\"error(timeout %d sec)\");",
! 122: SetTimer,comm,SetTimer);
! 123: }
1.1 takayama 124: if (quit) {
125: sprintf(comm,"GET /?msg=httpdAsirMeta+quit HTTP/1.1\n\n");
126: write(dataPort,comm,strlen(comm));
127: fflush(NULL);
128: }else{
1.2 takayama 129: sprintf(comm,"GET /?%s=%s;\n\n",key,byteArrayToUrlEncoding(asircomm,strlen(asircomm)));
1.1 takayama 130: write(dataPort,comm,strlen(comm));
131: fflush(NULL);
132: }
133:
134: /* get result */
135: for (i=0; i<SIZE; i++) comm[i]=0;
136: read(dataPort,comm,SIZE-2);
137: printf("%s\n",comm);
138: }
139:
1.2 takayama 140: /* from kan96xx/plugin/oxcgi.c */
141: /* . - _ A-Z a-z 0-9
142: space --> +
143: */
144: static int isUrlEncoding3(char s) {
145: if ((s == '.') || (s == '-') || (s == '_')) return(0);
146: if ((s >= 'A') && (s <= 'Z')) return(0);
147: if ((s >= 'a') && (s <= 'z')) return(0);
148: if ((s >= '0') && (s <= '9')) return(0);
149: if (s == ' ') return(0);
150: return(1);
151: }
152:
153: char *byteArrayToUrlEncoding(char *s,int size) {
154: int n,i,j;
155: char *r;
156: n = 0;
157: /* get Size */
158: for (i=0; i<size; i++) {
159: if (isUrlEncoding3((char)s[i])) n += 3;
160: n++;
161: }
162: r = malloc(n+1);
163: if (r == NULL) {fprintf(stderr,"%s\n","No more memory."); return(NULL); }
164: r[0] = 0; r[n] = 0;
165: i = 0; j = 0;
166: while ((j < n) && (i<size)) {
167: if (isUrlEncoding3((char)s[i])) {
168: sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
169: }else{
170: if ((char)s[i] == ' ') r[j]='+';
171: else r[j] = s[i];
172: j++; r[j] = 0;
173: }
174: i++;
175: }
176: return(r);
177: }
178:
179:
180: usage() {
181: fprintf(stderr,"webasir2 [--quit] [--asir command_string]\n");
1.3 ! takayama 182: fprintf(stderr," [--debug level]\n");
! 183: fprintf(stderr," [--settimer seconds]\n");
! 184: fprintf(stderr,"webasir2 --stdin ; command is obtained from the stdin.\n");
1.2 takayama 185: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>