=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/oxcgi.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -p -r1.1 -r1.2 --- OpenXM/src/kan96xx/plugin/oxcgi.c 2004/09/21 12:52:01 1.1 +++ OpenXM/src/kan96xx/plugin/oxcgi.c 2004/09/24 08:35:47 1.2 @@ -1 +1,523 @@ -/* $OpenXM$ */ +/* $OpenXM: OpenXM/src/kan96xx/plugin/oxcgi.c,v 1.1 2004/09/21 12:52:01 takayama Exp $ */ +#include +#include "datatype.h" +#include "stackm.h" +#include "extern.h" +#include "file2.h" +#include "oxcgi.h" + +static int ppp(char *s,int kstart,int kend, int vstart, int vend); +static int cgiHex(int p); + +static test1(); +static test2(); +static test3(); +static test4(); + +/* main() {KSstart();test4();} */ + +/* + [["URL","http://www.openxm.org"], + ["foo","value1"], <--- the first key value. + ["hoge","value2"] + ] + */ +struct object cgiUrlEncodingToKeyValuePair(char *s) { + int i,n,start; + int kstart,kend; /* start of key, end of key */ + int vstart,vend; /* start of value, end of value */ + int state; + int nOfPairs; + struct object rob; + struct object ob; + int k; + n = strlen(s); start = -1; + for (i=0; i + +*/ +static int isUrlEncoding3(char s) { + if ((s == '.') || (s == '-') || (s == '_')) return(0); + if ((s >= 'A') && (s <= 'Z')) return(0); + if ((s >= 'a') && (s <= 'z')) return(0); + if ((s >= '0') && (s <= '9')) return(0); + if (s == ' ') return(0); + return(1); +} + +char *byteArrayToUrlEncoding(unsigned char *s,int size) { + int n,i,j; + char *r; + n = 0; + /* get Size */ + for (i=0; i= '0' && p <= '9') return (p-'0'); + if (p >= 'A' && p <= 'F') return (p-'A'+10); + if (p >= 'a' && p <= 'f') return (p-'a'+10); + errorKan1("%s\n","Invalid argument to cgiHex."); +} + +/* for debug */ +static int ppp(char *s,int kstart,int kend, int vstart, int vend) { + int i; + printf("%d %d %d %d\n",kstart,kend,vstart,vend); + if (kstart >= 0) { + printf("key="); + for (i=kstart; i<=kend; i++) putchar(s[i]); + printf("\n"); + } + if (vstart >= 0) { + printf("value="); + for (i=vstart; i<=vend; i++) putchar(s[i]); + printf("\n"); + } +} +static test1() { + char s[1000]; + cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=asdfsdf&foo=asdfasdf"); + cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&"); + scanf("%s",s); + cgiUrlEncodingToKeyValuePair(s); +} +static test2() { + char s[1000]; + struct object ob; + ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=asdfsdf&foo=asdfasdf"); + printObject(ob,1,stdout); + ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&hoge=A%41+%42%62y%21"); + printObject(ob,1,stdout); + scanf("%s",s); + ob=cgiUrlEncodingToKeyValuePair(s); + printObject(ob,1,stdout); +} + +static test4() { + char s[1000]; + struct object ob; + char *ts; + int size; + ob=cgiUrlEncodingToKeyValuePair("http://hoge.hoge?name=1231232&hoge=&foo=asdfasdf&hoge=A%41+%42%62y%21"); + printObject(ob,1,stdout); + ts = cgiKeyValuePairToUrlEncoding(ob); + printf("result=%s",ts); + + + ts = "Pragma: no-cache\nContent-Length: 2915\nContent-Type: text/html\nConnection: close\n\n
\n

 \n

"; + + ob=cgiHttpToKeyValuePair(ts,strlen(ts)); + printObject(ob,1,stdout); + ts = cgiKeyValuePairToHttp(ob,&size); + printf("result:\n%s",ts); + +} +/* end for debug */ + + +char *cgiKeyValuePairToUrlEncoding(struct object ob) { + FILE2 *fp; + int size; + fp = fp2open(-1); + if (fp == NULL) errorKan1("%s\n","cgiKeyValuePairToUrlEncoding: open error."); + cgiKeyValuePairToUrlEncodingFile2(ob,fp); + return fp2fcloseInString(fp,&size); +} +int checkKeyValuePairFormat(struct object ob,char *msg) { + int i,n; + struct object eob,eob0,eob1; + static char *fmt = NULL; + int size; + char *ss; + size = 1024; + if (fmt == NULL) fmt = sGC_malloc(size); + if (fmt == NULL) errorKan1("%s\n","No more memory."); + for (i=0; i 1 ) fp2fputc('?',fp); + }else{ + fp2fputs(key,fp); fp2fputc('=',fp); + if (eob1.tag == Snull) ; + else if (eob1.tag == Sdollar) { + s = KopString(eob1); + fp2fputs(byteArrayToUrlEncoding((unsigned char *)s, strlen(s)),fp); + }else if (eob1.tag == SbyteArray) { + fp2fputs(byteArrayToUrlEncoding(KopByteArray(eob1),getByteArraySize(eob1)),fp); + }else{ + errorKan1("%s\n","Value is not string nor byte array."); + } + if (i < n-1) fp2fputc('&',fp); + } + } + return(fp2fflush(fp)); +} + +static struct object rStringToObj(char *s,int vstart,int vend,int mode) { + /* mode has not yet been used. */ + struct object rob; + char *sss; int i; + int bytearray; + bytearray=0; + if (vend < vstart) return NullObject; + for (i=vstart; i<= vend; i++) { + if (s[i] == 0) bytearray=1; + } + if (bytearray) { + rob = newByteArrayFromStr(&(s[vstart]),vend-vstart+1); + return(rob); + } + sss = (char *)sGC_malloc(vend-vstart+1); + if (sss == NULL) errorKan1("%s\n","No more memory."); + for (i=vstart; i<=vend; i++) { + sss[i-vstart] = s[i]; sss[i-vstart+1] = 0; + } + rob = KpoString(sss); + return(rob); +} + +/* + [["Content-Body$,"Body"], + ["key1","value1"], + ["key2","value2"], + ... + ] +*/ +struct object cgiHttpToKeyValuePair(char *s,int size) { + int ssize,i,j,k; + int nOfPairs, startbody,state, kstart,kend,vstart, vend,startline,endline; + int nextstart,path; + struct object rob,ob; + ssize = strlen(s); + nOfPairs = 0; startbody = -1; + /* state==0 : readline and set startline and endline; state = 1; + state==1 : if the line is empty, then state=10; + Determine kstart,kend (key) and vstart,vend (value); state=0; + state==10 : Read the body. + */ + for (path=0; path<2; path++) { + if (path == 1) { + rob = newObjectArray(nOfPairs+1); nOfPairs = 0; + } + i = 0; state=0; + while (i ' ') {kstart = j; break;} + } + for (j=kstart; j<=endline; j++) { + if (s[j] == ':') { kend=j-1; break; } + } + for (j=kend+2; j<=endline; j++) { + if (s[j] > ' ') {vstart = j; break; } + } + for (j=vend; j >= vstart; j--) { + if (s[j] > ' ') { vend=j; break;} + else vend = j-1; + } + /* ppp(s,kstart,kend,vstart,vend); */ + nOfPairs++; + if (path == 1) { + ob = newObjectArray(2); + putoa(ob,0,rStringToObj(s,kstart,kend,0)); + putoa(ob,1,rStringToObj(s,vstart,vend,0)); + putoa(rob,nOfPairs,ob); + } + state = 0; + } + }else { + startbody = i; + if (path == 1) { + ob = newObjectArray(2); + putoa(ob,0,KpoString("Content-Body")); + putoa(ob,1,rStringToObj(s,startbody,size-1,0)); + putoa(rob,0,ob); + } + break; + } + } + } + return rob; +} + +char *cgiKeyValuePairToHttp(struct object ob,int *sizep) { + char *s; + FILE2 *fp; + int size; + fp=fp2open(-1); + cgiKeyValuePairToHttpFile2(ob,fp); + s = fp2fcloseInString(fp,sizep); + return(s); +} + +int cgiKeyValuePairToHttpFile2(struct object ob,FILE2 *fp) { + int n,i; + struct object eob,eob0,eob1; + char *key, *s; + checkKeyValuePairFormat(ob,"cgiKeyValuePairToHttpFile2"); + n = getoaSize(ob); + if (n == 0) return(0); + for (i=1; i \n

"; + + ob=cgiHttpToKeyValuePair(s,strlen(s)); + printObject(ob,1,stdout); + + s = "Pragma:\nContent-Length: 2915\r\nContent-Type: text/html\nConnection: close\n\n
\n

 \n

"; + ob=cgiHttpToKeyValuePair(s,strlen(s)); + printObject(ob,1,stdout); + + s = "Pragma\nContent-Length 2915\r\nContent-Type text/html\nConnection: close\n\n
\n

 \n

"; + ob=cgiHttpToKeyValuePair(s,strlen(s)); + printObject(ob,1,stdout); + + { + char *s; + s = "This is a pen.com? !@#0989\n"; + printf("\n%s\n",byteArrayToUrlEncoding((unsigned char *)s,strlen(s))); + } +} + +struct object cgiKeyValuePairToUrlEncodingString(struct object ob) { + char *s; + s = cgiKeyValuePairToUrlEncoding(ob); + if (s == NULL) return NullObject; + return KpoString(s); +} +struct object cgiKeyValuePairToHttpString(struct object ob) { + int size; + char *s; + s = cgiKeyValuePairToHttp(ob,&size); + if (s == NULL) return NullObject; + return KpoString(s); +} +