[BACK]Return to oxdecode_post.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / trans

Annotation of OpenXM/src/kan96xx/trans/oxdecode_post.c, Revision 1.1

1.1     ! takayama    1: /* $OpenXM$
        !             2: Decompose a given multi-part post message to URL encoded one for cgi.sm1
        !             3: */
        !             4:
        !             5: /*
        !             6:   - use some codes in plugin/oxcgi.c
        !             7:     keyValuePairToUrlEncoding()
        !             8:   - doPolymake.OoHG does not work over the reverse proxy.
        !             9:   - data/testpost.sh is a test script for the input by curl.
        !            10:   - src/polymake/cgi/cgi-polymake.sh
        !            11: */
        !            12: #include <stdio.h>
        !            13: #include <string.h>
        !            14: #include <stdlib.h>
        !            15: int multipart(char *s);
        !            16: char *urlEncoding(char *s);
        !            17: int substr(char s[],int n,char a[]);
        !            18: char *byteArrayToUrlEncoding(unsigned char *s,int size);
        !            19: int isUrlEncoding3(char s);
        !            20: main() {
        !            21:   char *s;
        !            22:   int i,j,limit,len;
        !            23:   int c;
        !            24:   char slen[1024];
        !            25:   s=getenv("CONTENT_LENGTH");
        !            26:   if (s!=NULL) sscanf(s,"%d",&limit);
        !            27:   else limit=-1;
        !            28:   if (limit <= 0) {
        !            29:     fprintf(stderr,"CONTENT_LENGTH does not seem to be set.\n");
        !            30:     return(-1);
        !            31:   }
        !            32:   s = (char *)malloc(limit+1);
        !            33:   for (i=0; i<limit; i++) {
        !            34:     c = getchar();
        !            35:     if (c < 0) break; else s[i]=c;
        !            36:     s[i+1]=0;
        !            37:   }
        !            38:   if (!multipart(s)) {
        !            39:     for (i=0; i<limit; i++) putchar(s[i]);
        !            40:     return(0);
        !            41:   }
        !            42:   s = urlEncoding(s);
        !            43:   len=strlen(s);
        !            44:   sprintf(slen,"%d",len);
        !            45:   setenv("CONTENT_LENGTH",slen,1);
        !            46:   for (i=0; i<len; i++) putchar(s[i]);
        !            47:   /* need \n? */
        !            48: }
        !            49:
        !            50: /* if a is a substr of s, then it returns
        !            51:    the index i of s[] so that &(s[i]) agrees with a.
        !            52:    n is the size of s.
        !            53: */
        !            54: substr(char s[],int n,char a[]) {
        !            55:   int m,i,j;
        !            56:   m = strlen(a);
        !            57:   for (i=0; i<= n-m; i++) {  /*BUG(< --> <=). Make corrections for other use!*/
        !            58:        for (j=0; j<m; j++) {
        !            59:          if (s[i+j] != a[j]) break;
        !            60:          if (j == m-1) return i;
        !            61:        }
        !            62:   }
        !            63:   return -1;
        !            64: }
        !            65:
        !            66: /* bug, they should follow RFC */
        !            67: multipart(char *s) {
        !            68:   if ((strlen(s) > 3) && (strncmp(s,"---",3)==0)) return(1);
        !            69:   else return(0);
        !            70: }
        !            71:
        !            72: char *urlEncoding(char *s) {
        !            73:   int i,j,pos,pos2;
        !            74:   char *name;
        !            75:   char *body;
        !            76:   char *body2;
        !            77:   char *msg;
        !            78:   pos = substr(s,strlen(s),"name=");
        !            79:   if (pos < 0) return "error: no name=";
        !            80:   pos += 6; pos2=pos-1;
        !            81:   for (i=pos; i<strlen(s); i++) {
        !            82:     if (s[i] == '"') {
        !            83:       pos2 = i;
        !            84:       break;
        !            85:     }
        !            86:   }
        !            87:   if (pos2 < pos) return "error: name format error";
        !            88:   name = (char *) malloc(pos2-pos + 2);
        !            89:   for (i=0; i<pos2-pos; i++) {
        !            90:     name[i] = s[i+pos]; name[i+1]=0;
        !            91:   }
        !            92:
        !            93:   pos = pos2;
        !            94:   for (i=pos2; i<strlen(s); i++) {
        !            95:     if ((s[i] == '\n') && (s[i+1] == '\n')) {
        !            96:        pos = i+2; break;
        !            97:     }
        !            98:   }
        !            99:   pos2 = pos-1;
        !           100:   pos2=substr(&(s[pos]),strlen(s)-pos,"\n---");
        !           101:   if (pos2 < 0) return "error: body format error";
        !           102:   pos2++; pos2 += pos;
        !           103:   body = (char *) malloc(pos2-pos+2);
        !           104:   for (i=0; i<pos2-pos; i++) {
        !           105:     body[i] = s[i+pos]; body[i+1]=0;
        !           106:   }
        !           107:   /* we have obtained the name and body */
        !           108:   body2=byteArrayToUrlEncoding((unsigned char *)body,strlen(body));
        !           109:   msg = (char *)malloc(strlen(name)+strlen(body2)+5);
        !           110:   sprintf(msg,"%s=%s",name,body2);
        !           111:   return msg;
        !           112: }
        !           113:
        !           114:
        !           115: /* . - _  A-Z a-z 0-9
        !           116:    space --> +
        !           117: */
        !           118: int isUrlEncoding3(char s) {
        !           119:   if ((s == '.') || (s == '-') || (s == '_')) return(0);
        !           120:   if ((s >= 'A') && (s <= 'Z')) return(0);
        !           121:   if ((s >= 'a') && (s <= 'z')) return(0);
        !           122:   if ((s >= '0') && (s <= '9')) return(0);
        !           123:   if (s == ' ') return(0);
        !           124:   return(1);
        !           125: }
        !           126:
        !           127: char *byteArrayToUrlEncoding(unsigned char *s,int size) {
        !           128:   int n,i,j;
        !           129:   char *r;
        !           130:   n = 0;
        !           131:   /* get Size */
        !           132:   for (i=0; i<size; i++) {
        !           133:     if (isUrlEncoding3((char)s[i])) n += 3;
        !           134:     n++;
        !           135:   }
        !           136:   r = (char *)malloc(n+1);
        !           137:   r[0] = 0; r[n] = 0;
        !           138:   i = 0; j = 0;
        !           139:   while ((j < n) && (i<size)) {
        !           140:     if (isUrlEncoding3((char)s[i])) {
        !           141:       sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
        !           142:     }else{
        !           143:       if ((char)s[i] == ' ') r[j]='+';
        !           144:       else r[j] = s[i];
        !           145:       j++; r[j] = 0;
        !           146:     }
        !           147:     i++;
        !           148:   }
        !           149:   return(r);
        !           150: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>