Annotation of OpenXM/src/kan96xx/plugin/oxcgi.c, Revision 1.6
1.6 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxcgi.c,v 1.5 2004/09/28 12:20:40 takayama Exp $ */
1.2 takayama 2: #include <stdio.h>
3: #include "datatype.h"
4: #include "stackm.h"
5: #include "extern.h"
6: #include "file2.h"
7: #include "oxcgi.h"
8:
9: static int ppp(char *s,int kstart,int kend, int vstart, int vend);
10: static int cgiHex(int p);
11:
12: static test1();
13: static test2();
14: static test3();
15: static test4();
16:
17: /* main() {KSstart();test4();} */
18:
19: /*
20: [["URL","http://www.openxm.org"],
21: ["foo","value1"], <--- the first key value.
22: ["hoge","value2"]
23: ]
24: */
25: struct object cgiUrlEncodingToKeyValuePair(char *s) {
26: int i,n,start;
27: int kstart,kend; /* start of key, end of key */
28: int vstart,vend; /* start of value, end of value */
29: int state;
30: int nOfPairs;
31: struct object rob;
32: struct object ob;
33: int k;
34: n = strlen(s); start = -1;
35: for (i=0; i<n; i++) {
36: if (s[i] == '?') { start=i+1; break;}
37: }
1.5 takayama 38: if (start == -1) {
39: start = 0;
40: for (i=0; i<n; i++) {
41: if (s[i] > ' ') { start = i; break; }
42: }
43: }
1.2 takayama 44: for (k=0; k<2; k++) {
45: /* k==0 path one. Count nOfPairs. */
46: /* k==1 path two. generate array. */
47: nOfPairs = 0;
48: i = start;
49: /* state 0, 1 : key
50: =
51: state 2, 3 : value
52: &
53: state 5 : after getting &
54: state 4 : after getting eof
55: state 6
56: */
57: state = 0;
58: while (1) {
59: switch(state) {
60: case 0:
61: kstart = kend = vstart = vend = -1;
62: if (s[i] <= ' ') {
63: state=6; break;
64: } else {
65: kstart = kend = i;
66: nOfPairs++;
67: i++;
68: state=1;
69: break;
70: }
71: case 1:
72: if (s[i] <= ' ') {
73: state=4; break;
74: }else if (s[i] == '=') {
75: state=2; i++; break;
76: }else {
77: kend = i; i++; break;
78: }
79: case 2:
80: vstart = vend = -1;
81: if (s[i] <= ' ') {
82: state=4; break;
83: }else if (s[i] == '&') {
84: state=5; break;
85: }else {
86: state=3; vstart=vend=i; i++; break;
87: }
88: case 3:
89: if (s[i] <= ' ') {
90: state=4; break;
91: }else if (s[i] == '&') {
92: state=5; break;
93: }else{
94: vend=i; i++; break;
95: }
96: case 4:
97: if (k == 1) {
98: ob = newObjectArray(2);
99: putoa(ob,0,urlEncodedStringToObj(s,kstart,kend,0));
100: putoa(ob,1,urlEncodedStringToObj(s,vstart,vend,0));
101: putoa(rob,nOfPairs,ob);
102: }
103: state = -1; break;
104: case 5:
105: if (k == 1) {
106: ob = newObjectArray(2);
107: putoa(ob,0,urlEncodedStringToObj(s,kstart,kend,0));
108: putoa(ob,1,urlEncodedStringToObj(s,vstart,vend,0));
109: putoa(rob,nOfPairs,ob);
110: }
111: i++; state = 0; break;
112: case 6: state = -1; break;
113: default: break;
114: }
115: if (state < 0) break;
116: /*
117: if ((state == 4) || (state == 5)) {
118: ppp(s,kstart,kend,vstart,vend);
119: }
120: */
121: }
122: if (k == 0) {
123: char *stmp; int ii;
124: rob = newObjectArray(nOfPairs+1);
125: ob = newObjectArray(2);
126: putoa(ob,0,KpoString("URL"));
127: stmp = sGC_malloc(start+2);
128: if (stmp == NULL) errorKan1("%s\n","No more memory.");
129: stmp[0] = 0;
130: for (ii=0; ii<start-1; ii++) {
131: stmp[ii] = s[ii]; stmp[ii+1] = 0;
132: }
133: putoa(ob,1,KpoString(stmp));
134: putoa(rob,0,ob);
135: }
136: }
137: return rob;
138: }
139:
140: /* . - _ A-Z a-z 0-9
141: space --> +
142: */
143: static int isUrlEncoding3(char s) {
144: if ((s == '.') || (s == '-') || (s == '_')) return(0);
145: if ((s >= 'A') && (s <= 'Z')) return(0);
146: if ((s >= 'a') && (s <= 'z')) return(0);
147: if ((s >= '0') && (s <= '9')) return(0);
148: if (s == ' ') return(0);
149: return(1);
150: }
151:
152: char *byteArrayToUrlEncoding(unsigned char *s,int size) {
153: int n,i,j;
154: char *r;
155: n = 0;
156: /* get Size */
157: for (i=0; i<size; i++) {
158: if (isUrlEncoding3((char)s[i])) n += 3;
159: n++;
160: }
161: r = sGC_malloc(n+1);
162: if (r == NULL) errorKan1("%s\n","No more memory.");
163: r[0] = 0; r[n] = 0;
164: i = 0; j = 0;
165: while ((j < n) && (i<size)) {
166: if (isUrlEncoding3((char)s[i])) {
167: sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
168: }else{
169: if ((char)s[i] == ' ') r[j]='+';
170: else r[j] = s[i];
171: j++; r[j] = 0;
172: }
173: i++;
174: }
175: return(r);
176: }
177: struct object urlEncodedStringToObj(char *s,int vstart,int vend,int mode)
178: /*
179: mode == 0. Authmatically choose Sdollar or SbyteArray.
180: [ not implemented yet. ]
181: */
182: {
183: struct object rob;
184: char *ts;
185: char *ts2;
186: int i,j;
187: int p;
188: if ((s == NULL) || (vstart < 0)) return(NullObject);
189: if (vend+1-vstart <= 0) return(NullObject);
190: ts = (char *) malloc(vend-vstart+1);
191: if (ts == NULL) errorKan1("%s\n","Out of memory.");
192: j = 0; ts[j] = 0;
193: for (i=vstart; i<=vend; i++,j++) {
194: ts[j] = 0;
195: if (s[i] == '+') {
196: ts[j] = ' '; ts[j+1] = 0;
197: }else if (s[i] == '%') {
198: p = cgiHex(s[i+1])*16+cgiHex(s[i+2]);
199: i = i+2;
200: ts[j] = p; ts[j+1] = 0;
201: }else {
202: ts[j] = s[i]; ts[j+1] = 0;
203: }
204: }
205: ts2 = (char *)sGC_malloc(j);
206: if (ts2 == NULL) errorKan1("%s\n","Out of memory.");
207: for (i=0; i<j; i++) {
208: ts2[i] = ts[i];
209: }
210: return KpoString(ts2);
211: }
212:
213: static int cgiHex(int p) {
214: if (p >= '0' && p <= '9') return (p-'0');
215: if (p >= 'A' && p <= 'F') return (p-'A'+10);
216: if (p >= 'a' && p <= 'f') return (p-'a'+10);
217: errorKan1("%s\n","Invalid argument to cgiHex.");
218: }
219:
220: /* for debug */
221: static int ppp(char *s,int kstart,int kend, int vstart, int vend) {
222: int i;
223: printf("%d %d %d %d\n",kstart,kend,vstart,vend);
224: if (kstart >= 0) {
225: printf("key=");
226: for (i=kstart; i<=kend; i++) putchar(s[i]);
227: printf("\n");
228: }
229: if (vstart >= 0) {
230: printf("value=");
231: for (i=vstart; i<=vend; i++) putchar(s[i]);
232: printf("\n");
233: }
234: }
235: static test1() {
236: char s[1000];
237: cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=asdfsdf&foo=asdfasdf");
238: cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&");
239: scanf("%s",s);
240: cgiUrlEncodingToKeyValuePair(s);
241: }
242: static test2() {
243: char s[1000];
244: struct object ob;
245: ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=asdfsdf&foo=asdfasdf");
246: printObject(ob,1,stdout);
247: ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&hoge=A%41+%42%62y%21");
248: printObject(ob,1,stdout);
249: scanf("%s",s);
250: ob=cgiUrlEncodingToKeyValuePair(s);
251: printObject(ob,1,stdout);
252: }
253:
254: static test4() {
255: char s[1000];
256: struct object ob;
257: char *ts;
258: int size;
259: ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&hoge=A%41+%42%62y%21");
260: printObject(ob,1,stdout);
261: ts = cgiKeyValuePairToUrlEncoding(ob);
262: printf("result=%s",ts);
263:
264:
265: ts = "Pragma: no-cache\nContent-Length: 2915\nContent-Type: text/html\nConnection: close\n\n <DIV class=Section1> \n <P class=MsoNormal \n style=\"mso-list: none; mso-list-ins: \" 19991102T2025\"> \n </P> ";
266:
267: ob=cgiHttpToKeyValuePair(ts,strlen(ts));
268: printObject(ob,1,stdout);
269: ts = cgiKeyValuePairToHttp(ob,&size);
270: printf("result:\n%s",ts);
271:
272: }
273: /* end for debug */
274:
275:
276: char *cgiKeyValuePairToUrlEncoding(struct object ob) {
277: FILE2 *fp;
278: int size;
279: fp = fp2open(-1);
280: if (fp == NULL) errorKan1("%s\n","cgiKeyValuePairToUrlEncoding: open error.");
281: cgiKeyValuePairToUrlEncodingFile2(ob,fp);
282: return fp2fcloseInString(fp,&size);
283: }
284: int checkKeyValuePairFormat(struct object ob,char *msg) {
285: int i,n;
286: struct object eob,eob0,eob1;
287: static char *fmt = NULL;
288: int size;
289: char *ss;
290: size = 1024;
291: if (fmt == NULL) fmt = sGC_malloc(size);
292: if (fmt == NULL) errorKan1("%s\n","No more memory.");
293: for (i=0; i<size; i++) fmt[i]=0;
294: ss = "%s\n In ";
295: strcpy(fmt,ss);
296: strncpy(&(fmt[strlen(ss)]),msg,size-strlen(ss)-2);
297:
298: if (ob.tag != Sarray) errorKan1(fmt,"checkKeyValuePairFormat: argument must be an array.");
299: n = getoaSize(ob);
300: for (i=0; i<n ; i++) {
301: eob = getoa(ob,i);
302: if (eob.tag != Sarray) errorKan1(fmt,"checkKeyValuePairFormat: argument must be an array of arrays.");
303: if (getoaSize(eob) != 2) errorKan1(fmt,"checkKeyValuePairFormat: argument must be an array of arrays of size 2.");
304: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
305: if (eob0.tag != Sdollar) errorKan1(fmt,"checkKeyValuePairFormat: the key word must be a string.\n");
306: }
307: return 0;
308: }
309:
310: int cgiKeyValuePairToUrlEncodingFile2(struct object ob,FILE2 *fp) {
311: int n,i;
312: struct object eob,eob0,eob1;
313: char *key, *s;
314: checkKeyValuePairFormat(ob,"cgiKeyValuePairToUrlEncodingFile2");
315: n = getoaSize(ob);
316: for (i=0; i<n; i++) {
317: eob = getoa(ob,i);
318: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
319: key = KopString(eob0);
320: if (i == 0) {
321: if (strcmp(key,"URL") != 0) warningKan("Key word should be URL.\n");
322: if (eob1.tag != Sdollar) errorKan1("%s\n","URL value must be a string.");
323: fp2fputs(KopString(eob1),fp);
324: if ( n > 1 ) fp2fputc('?',fp);
325: }else{
326: fp2fputs(key,fp); fp2fputc('=',fp);
327: if (eob1.tag == Snull) ;
328: else if (eob1.tag == Sdollar) {
329: s = KopString(eob1);
330: fp2fputs(byteArrayToUrlEncoding((unsigned char *)s, strlen(s)),fp);
331: }else if (eob1.tag == SbyteArray) {
332: fp2fputs(byteArrayToUrlEncoding(KopByteArray(eob1),getByteArraySize(eob1)),fp);
333: }else{
334: errorKan1("%s\n","Value is not string nor byte array.");
335: }
336: if (i < n-1) fp2fputc('&',fp);
337: }
338: }
339: return(fp2fflush(fp));
340: }
341:
342: static struct object rStringToObj(char *s,int vstart,int vend,int mode) {
343: /* mode has not yet been used. */
344: struct object rob;
345: char *sss; int i;
346: int bytearray;
347: bytearray=0;
348: if (vend < vstart) return NullObject;
349: for (i=vstart; i<= vend; i++) {
350: if (s[i] == 0) bytearray=1;
351: }
352: if (bytearray) {
353: rob = newByteArrayFromStr(&(s[vstart]),vend-vstart+1);
354: return(rob);
355: }
356: sss = (char *)sGC_malloc(vend-vstart+1);
357: if (sss == NULL) errorKan1("%s\n","No more memory.");
358: for (i=vstart; i<=vend; i++) {
359: sss[i-vstart] = s[i]; sss[i-vstart+1] = 0;
360: }
361: rob = KpoString(sss);
362: return(rob);
363: }
364:
365: /*
366: [["Content-Body$,"Body"],
367: ["key1","value1"],
368: ["key2","value2"],
369: ...
370: ]
371: */
372: struct object cgiHttpToKeyValuePair(char *s,int size) {
373: int ssize,i,j,k;
374: int nOfPairs, startbody,state, kstart,kend,vstart, vend,startline,endline;
375: int nextstart,path;
376: struct object rob,ob;
377: ssize = strlen(s);
378: nOfPairs = 0; startbody = -1;
379: /* state==0 : readline and set startline and endline; state = 1;
380: state==1 : if the line is empty, then state=10;
381: Determine kstart,kend (key) and vstart,vend (value); state=0;
382: state==10 : Read the body.
383: */
384: for (path=0; path<2; path++) {
385: if (path == 1) {
386: rob = newObjectArray(nOfPairs+1); nOfPairs = 0;
387: }
388: i = 0; state=0;
389: while (i<size) {
390: if (state == 0) {
391: /* Read the line */
392: startline=i; endline= size-1; nextstart = size;
393: for (j = startline; j<size; j++) {
394: if (s[j] == 0xd) {
395: endline = j-1;
396: if (s[j+1] == 0xa) nextstart = j+2;
397: else nextstart = j+1;
398: break;
399: }else if (s[j] == 0xa) {
400: endline = j-1; nextstart = j+1; break;
401: }
402: }
403: state = 1; i = nextstart;
404: }else if (state == 1) {
405: if (endline <= startline) { state=10; }
406: else {
407: kstart=startline; kend = endline;
408: vstart=endline+1; vend = endline;
409: for (j=startline; j<=endline; j++) {
410: if (s[j] > ' ') {kstart = j; break;}
411: }
412: for (j=kstart; j<=endline; j++) {
413: if (s[j] == ':') { kend=j-1; break; }
414: }
415: for (j=kend+2; j<=endline; j++) {
416: if (s[j] > ' ') {vstart = j; break; }
417: }
418: for (j=vend; j >= vstart; j--) {
419: if (s[j] > ' ') { vend=j; break;}
420: else vend = j-1;
421: }
422: /* ppp(s,kstart,kend,vstart,vend); */
423: nOfPairs++;
424: if (path == 1) {
425: ob = newObjectArray(2);
426: putoa(ob,0,rStringToObj(s,kstart,kend,0));
427: putoa(ob,1,rStringToObj(s,vstart,vend,0));
428: putoa(rob,nOfPairs,ob);
429: }
430: state = 0;
431: }
432: }else {
433: startbody = i;
434: if (path == 1) {
435: ob = newObjectArray(2);
436: putoa(ob,0,KpoString("Content-Body"));
437: putoa(ob,1,rStringToObj(s,startbody,size-1,0));
438: putoa(rob,0,ob);
439: }
440: break;
441: }
442: }
443: }
444: return rob;
445: }
446:
447: char *cgiKeyValuePairToHttp(struct object ob,int *sizep) {
448: char *s;
449: FILE2 *fp;
450: int size;
451: fp=fp2open(-1);
452: cgiKeyValuePairToHttpFile2(ob,fp);
453: s = fp2fcloseInString(fp,sizep);
454: return(s);
455: }
456:
457: int cgiKeyValuePairToHttpFile2(struct object ob,FILE2 *fp) {
458: int n,i;
459: struct object eob,eob0,eob1;
460: char *key, *s;
461: checkKeyValuePairFormat(ob,"cgiKeyValuePairToHttpFile2");
462: n = getoaSize(ob);
463: if (n == 0) return(0);
464: for (i=1; i<n; i++) {
465: eob = getoa(ob,i);
466: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
467: key = KopString(eob0);
468: fp2fputs(key,fp); fp2fputs(": ",fp);
469: if (eob1.tag == Snull) ;
470: else if (eob1.tag == Sdollar) {
471: s = KopString(eob1);
472: fp2fputs(s,fp);
473: }else{
474: errorKan1("%s\n","Value is not a string.");
475: }
476: if (i < n-1) fp2fputc('\n',fp);
477: else fp2fputs("\n\n",fp);
478: }
479: eob = getoa(ob,0);
480: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
481: key = KopString(eob0);
482: if (strcmp(key,"Content-Body") != 0) warningKan("Key word should be Content-Body.\n");
483: if (eob1.tag == Sdollar) {
484: fp2fputs(KopString(eob1),fp);
485: }else if (eob1.tag == SbyteArray) {
486: fp2write(fp,KopByteArray(eob1),getByteArraySize(eob1));
487: }else errorKan1("%s\n","BODY must be a string or a byte array.");
488:
489: return(fp2fflush(fp));
490: }
491:
492:
493: static test3() {
494: char *s;
495: struct object ob;
496: s = "Pragma: no-cache\nContent-Length: 2915\nContent-Type: text/html\nConnection: close\n\n <DIV class=Section1> \n <P class=MsoNormal \n style=\"mso-list: none; mso-list-ins: \" 19991102T2025\"> \n </P> ";
497:
498: ob=cgiHttpToKeyValuePair(s,strlen(s));
499: printObject(ob,1,stdout);
500:
501: s = "Pragma:\nContent-Length: 2915\r\nContent-Type: text/html\nConnection: close\n\n <DIV class=Section1> \n <P class=MsoNormal \n style=\"mso-list: none; mso-list-ins: \" 19991102T2025\"> \n </P> ";
502: ob=cgiHttpToKeyValuePair(s,strlen(s));
503: printObject(ob,1,stdout);
504:
505: s = "Pragma\nContent-Length 2915\r\nContent-Type text/html\nConnection: close\n\n <DIV class=Section1> \n <P class=MsoNormal \n style=\"mso-list: none; mso-list-ins: \" 19991102T2025\"> \n </P> ";
506: ob=cgiHttpToKeyValuePair(s,strlen(s));
507: printObject(ob,1,stdout);
508:
509: {
510: char *s;
511: s = "This is a pen.com? !@#0989\n";
512: printf("\n%s\n",byteArrayToUrlEncoding((unsigned char *)s,strlen(s)));
513: }
514: }
515:
516: struct object cgiKeyValuePairToUrlEncodingString(struct object ob) {
517: char *s;
518: s = cgiKeyValuePairToUrlEncoding(ob);
519: if (s == NULL) return NullObject;
520: return KpoString(s);
521: }
522: struct object cgiKeyValuePairToHttpString(struct object ob) {
523: int size;
524: char *s;
525: s = cgiKeyValuePairToHttp(ob,&size);
526: if (s == NULL) return NullObject;
527: return KpoString(s);
528: }
529:
1.3 takayama 530: struct object KooStringToUrlEncoding(struct object sob) {
531: unsigned char *s;
532: char *rs;
533: int n;
534: if (sob.tag == Sdollar) {
535: s = (unsigned char *) KopString(sob);
536: n = strlen((char *)s);
537: }else if (sob.tag == SbyteArray) {
538: s = KopByteArray(sob);
539: n = getByteArraySize(sob);
540: }else errorKan1("%s\n","KooStringToUrlEncoding: argument must be a string or a bytearray.");
541: rs = byteArrayToUrlEncoding(s,n);
542: return KpoString(rs);
543: }
544:
545: struct object KooUrlEncodedStringToObj(struct object sob) {
546: char *s;
547: int n;
548: if (sob.tag == Sdollar) {
549: s = KopString(sob);
550: n = strlen((char *)s);
551: }else if (sob.tag == SbyteArray) {
552: s = KopByteArray(sob);
553: n = getByteArraySize(sob);
554: }else errorKan1("%s\n","KooUrlEncodedStringToObj: argument must be a string.");
555: return urlEncodedStringToObj(s,0,n-1,0);
556: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>