Annotation of OpenXM/src/asir-port/cgi/webasir2.c, Revision 1.2
1.2 ! takayama 1: /* $OpenXM: OpenXM/src/asir-port/cgi/webasir2.c,v 1.1 2014/08/30 12:25:56 takayama Exp $
1.1 takayama 2: */
3: /*
4: (httpd-asir2.sm1) run webasir2
5: */
6: #include <stdio.h>
1.2 ! takayama 7: #include <stdlib.h>
1.1 takayama 8: #include <sys/types.h>
9: #include <sys/socket.h>
10: #include <sys/time.h>
11: #include <netinet/in.h>
12: #include <netdb.h>
13: #include <string.h>
14:
15: #define SIZE 0x10000
1.2 ! takayama 16: char *byteArrayToUrlEncoding(char *s,int size);
1.1 takayama 17:
18: int Debug=1;
19: int main(int argc,char *argv[]) {
20: int dataPort;
21: struct hostent *servhost;
22: struct sockaddr_in dServer;
23: FILE *fp;
24: char s[1];
25: char fname[SIZE];
26: int i;
27: char key[SIZE];
28: char comm[SIZE];
1.2 ! takayama 29: char *asircomm;
1.1 takayama 30: int quit;
31: quit = 0;
1.2 ! takayama 32: asircomm="3-2;";
1.1 takayama 33: for (i=1; i<argc; i++) {
34: if (strcmp(argv[i],"--quit")==0) quit=1;
1.2 ! takayama 35: else if (strcmp(argv[i],"--asir")==0) {
! 36: i++;
! 37: if (i <argc) asircomm = argv[i];
! 38: else { usage(); return(-1); }
! 39: }
1.1 takayama 40: }
41:
42: system("ls /tmp/webasir*.txt >tmp-webasir.txt");
43: fp = fopen("tmp-webasir.txt","r");
44: if (fp == NULL) {
45: fprintf(stderr,"No webasir2 is running.\n"); return(-1);
46: }
47: fgets(fname,SIZE-2,fp);
48: for (i=strlen(fname)-1; i>=0; i--) {
49: if (fname[i] <= ' ') fname[i]=0; else break;
50: }
51: fclose(fp);
52: if (strlen(fname)==0) {
53: fprintf(stderr,"No webasir2 pid file.\n"); return(-1);
54: }
55: fp = fopen(fname,"r");
56: if (fp == NULL) {
57: fprintf(stderr,"Open error of %s\n",fname); return(-1);
58: }
59: fgets(key,SIZE-2,fp); sscanf(key,"%d",&dataPort);
60: if (Debug) printf("dataPort=%d\n",dataPort);
61: fgets(key,SIZE-2,fp);
62: for (i=strlen(key)-1; i>=0; i--) {
63: if (key[i] <= ' ') key[i]=0; else break;
64: }
65: if (Debug) printf("key=%s\n",key);
66: fclose(fp);
67:
68: if ((servhost = gethostbyname("localhost")) == NULL) {
69: fprintf(stderr,"bad server name.\n"); return(-1);
70: }
71:
72: /* Connecting to the data port */
73: bzero((char *)&dServer,sizeof(dServer));
74: dServer.sin_family = AF_INET;
75: dServer.sin_port = htons(dataPort);
76: bcopy(servhost->h_addr,
77: (char *)&dServer.sin_addr,servhost->h_length);
78: if ((dataPort = socket(AF_INET,SOCK_STREAM,0)) <0) {
79: fprintf(stderr,"socket allocation is failed.\n");
80: }
81: if (Debug) fprintf(stderr,"Trying to connect port %d ",ntohs(dServer.sin_port));
82:
83: if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) {
84: fprintf(stderr,"cannot connect\n");
85: }else{ fprintf(stderr,"Connected\n"); }
86:
87: if (quit) {
88: sprintf(comm,"GET /?msg=httpdAsirMeta+quit HTTP/1.1\n\n");
89: write(dataPort,comm,strlen(comm));
90: fflush(NULL);
91: }else{
1.2 ! takayama 92: sprintf(comm,"GET /?%s=%s;\n\n",key,byteArrayToUrlEncoding(asircomm,strlen(asircomm)));
1.1 takayama 93: write(dataPort,comm,strlen(comm));
94: fflush(NULL);
95: }
96:
97: /* get result */
98: for (i=0; i<SIZE; i++) comm[i]=0;
99: read(dataPort,comm,SIZE-2);
100: printf("%s\n",comm);
101: }
102:
1.2 ! takayama 103: /* from kan96xx/plugin/oxcgi.c */
! 104: /* . - _ A-Z a-z 0-9
! 105: space --> +
! 106: */
! 107: static int isUrlEncoding3(char s) {
! 108: if ((s == '.') || (s == '-') || (s == '_')) return(0);
! 109: if ((s >= 'A') && (s <= 'Z')) return(0);
! 110: if ((s >= 'a') && (s <= 'z')) return(0);
! 111: if ((s >= '0') && (s <= '9')) return(0);
! 112: if (s == ' ') return(0);
! 113: return(1);
! 114: }
! 115:
! 116: char *byteArrayToUrlEncoding(char *s,int size) {
! 117: int n,i,j;
! 118: char *r;
! 119: n = 0;
! 120: /* get Size */
! 121: for (i=0; i<size; i++) {
! 122: if (isUrlEncoding3((char)s[i])) n += 3;
! 123: n++;
! 124: }
! 125: r = malloc(n+1);
! 126: if (r == NULL) {fprintf(stderr,"%s\n","No more memory."); return(NULL); }
! 127: r[0] = 0; r[n] = 0;
! 128: i = 0; j = 0;
! 129: while ((j < n) && (i<size)) {
! 130: if (isUrlEncoding3((char)s[i])) {
! 131: sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
! 132: }else{
! 133: if ((char)s[i] == ' ') r[j]='+';
! 134: else r[j] = s[i];
! 135: j++; r[j] = 0;
! 136: }
! 137: i++;
! 138: }
! 139: return(r);
! 140: }
! 141:
! 142:
! 143: usage() {
! 144: fprintf(stderr,"webasir2 [--quit] [--asir command_string]\n");
! 145: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>