Annotation of OpenXM/src/kan96xx/plugin/oxcgi.c, Revision 1.8
1.8 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxcgi.c,v 1.7 2004/11/23 01:37:47 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);
1.7 takayama 320: if ((i == 0) && (strcmp(key,"URL")==0)) {
1.2 takayama 321: if (eob1.tag != Sdollar) errorKan1("%s\n","URL value must be a string.");
322: fp2fputs(KopString(eob1),fp);
323: if ( n > 1 ) fp2fputc('?',fp);
324: }else{
325: fp2fputs(key,fp); fp2fputc('=',fp);
326: if (eob1.tag == Snull) ;
327: else if (eob1.tag == Sdollar) {
328: s = KopString(eob1);
329: fp2fputs(byteArrayToUrlEncoding((unsigned char *)s, strlen(s)),fp);
330: }else if (eob1.tag == SbyteArray) {
331: fp2fputs(byteArrayToUrlEncoding(KopByteArray(eob1),getByteArraySize(eob1)),fp);
332: }else{
333: errorKan1("%s\n","Value is not string nor byte array.");
334: }
335: if (i < n-1) fp2fputc('&',fp);
336: }
337: }
338: return(fp2fflush(fp));
339: }
340:
341: static struct object rStringToObj(char *s,int vstart,int vend,int mode) {
342: /* mode has not yet been used. */
343: struct object rob;
344: char *sss; int i;
345: int bytearray;
346: bytearray=0;
347: if (vend < vstart) return NullObject;
348: for (i=vstart; i<= vend; i++) {
349: if (s[i] == 0) bytearray=1;
350: }
351: if (bytearray) {
352: rob = newByteArrayFromStr(&(s[vstart]),vend-vstart+1);
353: return(rob);
354: }
355: sss = (char *)sGC_malloc(vend-vstart+1);
356: if (sss == NULL) errorKan1("%s\n","No more memory.");
357: for (i=vstart; i<=vend; i++) {
358: sss[i-vstart] = s[i]; sss[i-vstart+1] = 0;
359: }
360: rob = KpoString(sss);
361: return(rob);
362: }
363:
364: /*
365: [["Content-Body$,"Body"],
366: ["key1","value1"],
367: ["key2","value2"],
368: ...
369: ]
370: */
371: struct object cgiHttpToKeyValuePair(char *s,int size) {
372: int ssize,i,j,k;
373: int nOfPairs, startbody,state, kstart,kend,vstart, vend,startline,endline;
374: int nextstart,path;
375: struct object rob,ob;
376: ssize = strlen(s);
377: nOfPairs = 0; startbody = -1;
378: /* state==0 : readline and set startline and endline; state = 1;
379: state==1 : if the line is empty, then state=10;
380: Determine kstart,kend (key) and vstart,vend (value); state=0;
381: state==10 : Read the body.
382: */
383: for (path=0; path<2; path++) {
384: if (path == 1) {
385: rob = newObjectArray(nOfPairs+1); nOfPairs = 0;
386: }
387: i = 0; state=0;
388: while (i<size) {
389: if (state == 0) {
390: /* Read the line */
391: startline=i; endline= size-1; nextstart = size;
392: for (j = startline; j<size; j++) {
393: if (s[j] == 0xd) {
394: endline = j-1;
395: if (s[j+1] == 0xa) nextstart = j+2;
396: else nextstart = j+1;
397: break;
398: }else if (s[j] == 0xa) {
399: endline = j-1; nextstart = j+1; break;
400: }
401: }
402: state = 1; i = nextstart;
403: }else if (state == 1) {
404: if (endline <= startline) { state=10; }
405: else {
406: kstart=startline; kend = endline;
407: vstart=endline+1; vend = endline;
408: for (j=startline; j<=endline; j++) {
409: if (s[j] > ' ') {kstart = j; break;}
410: }
411: for (j=kstart; j<=endline; j++) {
412: if (s[j] == ':') { kend=j-1; break; }
413: }
414: for (j=kend+2; j<=endline; j++) {
415: if (s[j] > ' ') {vstart = j; break; }
416: }
417: for (j=vend; j >= vstart; j--) {
418: if (s[j] > ' ') { vend=j; break;}
419: else vend = j-1;
420: }
421: /* ppp(s,kstart,kend,vstart,vend); */
422: nOfPairs++;
423: if (path == 1) {
424: ob = newObjectArray(2);
425: putoa(ob,0,rStringToObj(s,kstart,kend,0));
426: putoa(ob,1,rStringToObj(s,vstart,vend,0));
427: putoa(rob,nOfPairs,ob);
428: }
429: state = 0;
430: }
431: }else {
432: startbody = i;
433: if (path == 1) {
434: ob = newObjectArray(2);
435: putoa(ob,0,KpoString("Content-Body"));
436: putoa(ob,1,rStringToObj(s,startbody,size-1,0));
437: putoa(rob,0,ob);
438: }
439: break;
440: }
441: }
442: }
443: return rob;
444: }
445:
446: char *cgiKeyValuePairToHttp(struct object ob,int *sizep) {
447: char *s;
448: FILE2 *fp;
449: int size;
450: fp=fp2open(-1);
451: cgiKeyValuePairToHttpFile2(ob,fp);
452: s = fp2fcloseInString(fp,sizep);
453: return(s);
454: }
455:
456: int cgiKeyValuePairToHttpFile2(struct object ob,FILE2 *fp) {
457: int n,i;
458: struct object eob,eob0,eob1;
459: char *key, *s;
460: checkKeyValuePairFormat(ob,"cgiKeyValuePairToHttpFile2");
461: n = getoaSize(ob);
462: if (n == 0) return(0);
463: for (i=1; i<n; i++) {
464: eob = getoa(ob,i);
465: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
466: key = KopString(eob0);
467: fp2fputs(key,fp); fp2fputs(": ",fp);
468: if (eob1.tag == Snull) ;
469: else if (eob1.tag == Sdollar) {
470: s = KopString(eob1);
471: fp2fputs(s,fp);
472: }else{
473: errorKan1("%s\n","Value is not a string.");
474: }
475: if (i < n-1) fp2fputc('\n',fp);
476: else fp2fputs("\n\n",fp);
477: }
478: eob = getoa(ob,0);
479: eob0 = getoa(eob,0); eob1 = getoa(eob,1);
480: key = KopString(eob0);
481: if (strcmp(key,"Content-Body") != 0) warningKan("Key word should be Content-Body.\n");
482: if (eob1.tag == Sdollar) {
483: fp2fputs(KopString(eob1),fp);
484: }else if (eob1.tag == SbyteArray) {
485: fp2write(fp,KopByteArray(eob1),getByteArraySize(eob1));
486: }else errorKan1("%s\n","BODY must be a string or a byte array.");
487:
488: return(fp2fflush(fp));
489: }
490:
491:
492: static test3() {
493: char *s;
494: struct object ob;
495: 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> ";
496:
497: ob=cgiHttpToKeyValuePair(s,strlen(s));
498: printObject(ob,1,stdout);
499:
500: 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> ";
501: ob=cgiHttpToKeyValuePair(s,strlen(s));
502: printObject(ob,1,stdout);
503:
504: 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> ";
505: ob=cgiHttpToKeyValuePair(s,strlen(s));
506: printObject(ob,1,stdout);
507:
508: {
509: char *s;
510: s = "This is a pen.com? !@#0989\n";
511: printf("\n%s\n",byteArrayToUrlEncoding((unsigned char *)s,strlen(s)));
512: }
513: }
514:
515: struct object cgiKeyValuePairToUrlEncodingString(struct object ob) {
516: char *s;
517: s = cgiKeyValuePairToUrlEncoding(ob);
518: if (s == NULL) return NullObject;
519: return KpoString(s);
520: }
521: struct object cgiKeyValuePairToHttpString(struct object ob) {
522: int size;
523: char *s;
524: s = cgiKeyValuePairToHttp(ob,&size);
525: if (s == NULL) return NullObject;
526: return KpoString(s);
527: }
528:
1.3 takayama 529: struct object KooStringToUrlEncoding(struct object sob) {
530: unsigned char *s;
531: char *rs;
532: int n;
533: if (sob.tag == Sdollar) {
534: s = (unsigned char *) KopString(sob);
535: n = strlen((char *)s);
536: }else if (sob.tag == SbyteArray) {
537: s = KopByteArray(sob);
538: n = getByteArraySize(sob);
539: }else errorKan1("%s\n","KooStringToUrlEncoding: argument must be a string or a bytearray.");
540: rs = byteArrayToUrlEncoding(s,n);
541: return KpoString(rs);
542: }
543:
544: struct object KooUrlEncodedStringToObj(struct object sob) {
545: char *s;
546: int n;
547: if (sob.tag == Sdollar) {
548: s = KopString(sob);
549: n = strlen((char *)s);
550: }else if (sob.tag == SbyteArray) {
551: s = KopByteArray(sob);
552: n = getByteArraySize(sob);
553: }else errorKan1("%s\n","KooUrlEncodedStringToObj: argument must be a string.");
554: return urlEncodedStringToObj(s,0,n-1,0);
1.8 ! takayama 555: }
! 556:
! 557: static struct object toTokens(char *s,int *sep,int nsep) {
! 558: /* s is the input, and sep are the separators. */
! 559: /* -1 means <=' ' are separators */
! 560: int nOfTokens,n,i,done,k,start,sav;
! 561: struct object rob;
! 562: char *t;
! 563:
! 564: rob = NullObject;
! 565: if (nsep < 1) return rob;
! 566: if (sep[0] != -1) {
! 567: fprintf(stderr,"cgiToTokens: Not implemeted for this separator.\n");
! 568: return rob;
! 569: }
! 570:
! 571: /* Count the number of tokens */
! 572: n = strlen(s); i = 0; nOfTokens=0;
! 573: while (i < n) {
! 574: done = 0;
! 575: while (s[i] <= ' ') {
! 576: i++; if (i >= n) { done=1; break;}
! 577: }
! 578: if (done==1) break;
! 579: nOfTokens++;
! 580: while (s[i] > ' ') {
! 581: i++; if (i >= n) { done=1; break; }
! 582: }
! 583: if (done == 1) break;
! 584: }
! 585:
! 586: rob = newObjectArray(nOfTokens);
! 587: n = strlen(s); i = 0; k = 0;
! 588: while (i < n) {
! 589: done = 0;
! 590: while (s[i] <= ' ') {
! 591: i++; if (i >= n) { done=1; break;}
! 592: }
! 593: if (done==1) break;
! 594: start = i;
! 595: while (s[i] > ' ') {
! 596: i++; if (i >= n) { done=1; break; }
! 597: }
! 598: t = (char *) GC_malloc(i-start+1);
! 599: if (t == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
! 600: t[i-start] = 0;
! 601: strncpy(t,&(s[start]),i-start);
! 602: putoa(rob,k,KpoString(t));
! 603: k++;
! 604: if (done == 1) break;
! 605: }
! 606:
! 607: return rob;
! 608: }
! 609:
! 610: struct object KooToTokens(struct object ob,struct object sep) {
! 611: char *s;
! 612: int n;
! 613: int tmp[1];
! 614: tmp[0] = -1;
! 615: if (ob.tag == Sdollar) {
! 616: s = KopString(ob);
! 617: n = strlen((char *)s);
! 618: }else errorKan1("%s\n","KooToTokens: the first argument must be a string.");
! 619: if (sep.tag == Sarray) {
! 620: if (getoaSize(sep) != 0) {
! 621: errorKan1("%s\n","This separators have not been implemented.");
! 622: }
! 623: }else errorKan1("%s\n","KooToTokens: the second argument(separators) must be an array.");
! 624: return toTokens(s,tmp,1);
1.3 takayama 625: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>