[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.3

1.3     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/trans/oxdecode_post.c,v 1.2 2013/09/22 02:38:25 takayama Exp $
1.1       takayama    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);
1.3     ! takayama   20: int main() {
1.1       takayama   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: */
1.3     ! takayama   54: int substr(char s[],int n,char a[]) {
1.1       takayama   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 */
1.3     ! takayama   67: int multipart(char *s) {
1.1       takayama   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++) {
1.2       takayama   95:     if ((s[i] == '\n') && (s[i+1] == '\n')) { /* \n\n */
1.1       takayama   96:        pos = i+2; break;
                     97:     }
1.2       takayama   98:     if ((s[i] == 0xd) && (s[i+1] == '\n') && (s[i+2] == 0xd) && (s[i+3] == '\n')) { /* 0d, 0x */
                     99:        pos = i+4; break;
                    100:     }
1.1       takayama  101:   }
                    102:   pos2 = pos-1;
                    103:   pos2=substr(&(s[pos]),strlen(s)-pos,"\n---");
                    104:   if (pos2 < 0) return "error: body format error";
                    105:   pos2++; pos2 += pos;
                    106:   body = (char *) malloc(pos2-pos+2);
                    107:   for (i=0; i<pos2-pos; i++) {
                    108:     body[i] = s[i+pos]; body[i+1]=0;
                    109:   }
                    110:   /* we have obtained the name and body */
                    111:   body2=byteArrayToUrlEncoding((unsigned char *)body,strlen(body));
                    112:   msg = (char *)malloc(strlen(name)+strlen(body2)+5);
                    113:   sprintf(msg,"%s=%s",name,body2);
                    114:   return msg;
                    115: }
                    116:
                    117:
                    118: /* . - _  A-Z a-z 0-9
                    119:    space --> +
                    120: */
                    121: int isUrlEncoding3(char s) {
                    122:   if ((s == '.') || (s == '-') || (s == '_')) return(0);
                    123:   if ((s >= 'A') && (s <= 'Z')) return(0);
                    124:   if ((s >= 'a') && (s <= 'z')) return(0);
                    125:   if ((s >= '0') && (s <= '9')) return(0);
                    126:   if (s == ' ') return(0);
                    127:   return(1);
                    128: }
                    129:
                    130: char *byteArrayToUrlEncoding(unsigned char *s,int size) {
                    131:   int n,i,j;
                    132:   char *r;
                    133:   n = 0;
                    134:   /* get Size */
                    135:   for (i=0; i<size; i++) {
                    136:     if (isUrlEncoding3((char)s[i])) n += 3;
                    137:     n++;
                    138:   }
                    139:   r = (char *)malloc(n+1);
                    140:   r[0] = 0; r[n] = 0;
                    141:   i = 0; j = 0;
                    142:   while ((j < n) && (i<size)) {
                    143:     if (isUrlEncoding3((char)s[i])) {
                    144:       sprintf(&(r[j]),"%%%02X",s[i]); j += 3;
                    145:     }else{
                    146:       if ((char)s[i] == ' ') r[j]='+';
                    147:       else r[j] = s[i];
                    148:       j++; r[j] = 0;
                    149:     }
                    150:     i++;
                    151:   }
                    152:   return(r);
                    153: }

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