Annotation of parser3/src/types/pa_vform.C, revision 1.8

1.1       paf         1: /*
                      2:        Parser
1.3       paf         3:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
                      4:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         5: 
1.8     ! paf         6:        $Id: pa_vform.C,v 1.7 2001/03/19 15:29:42 paf Exp $
1.1       paf         7: */
                      8: 
1.7       paf         9: /**
                     10:        \todo based on cgic by ?
1.3       paf        11: */
                     12: 
                     13: #include <ctype.h>
                     14: #include <string.h>
                     15: 
1.1       paf        16: #include "pa_vform.h"
                     17: #include "pa_vstring.h"
1.3       paf        18: #include "pa_globals.h"
                     19: #include "pa_request.h"
                     20: 
                     21: // parse helper funcs
                     22: 
                     23: static bool StrEqNc(const char *s1, const char *s2, bool strict) {
                     24:        while(true) {
                     25:                if(!(*s1)) {
                     26:                        if(!(*s2))
                     27:                                return true;
                     28:                        else
                     29:                                return !strict;
                     30:                } else if(!(*s2))
                     31:                        return !strict;
                     32:                if(isalpha(*s1)) {
                     33:                        if(tolower(*s1) !=tolower(*s2))
                     34:                                return false;
                     35:                } else if((*s1) !=(*s2))
                     36:                        return false;
                     37:                s1++;
                     38:                s2++;
                     39:        }
                     40: }
                     41: 
                     42: static int getHeader(const char *data,int len){
                     43:     int i,enter=-1;
                     44:     if (data)
                     45:        for (i=0;i<len;i++)
                     46:            if (data[i]=='\n'){
                     47:                if (enter>=0) enter++;
                     48:                if (enter>1) return i;
                     49:            } else if (data[i]!='\r') enter=0;
                     50:     return 0;
                     51: }
                     52: 
                     53: static const char *searchAttribute(const char *data,const char *attr,int len){
                     54:     int i;
                     55:     if (data)
                     56:        for (i=0;i<len;i++)
                     57:            if (tolower(data[i])==*attr){
                     58:                int j;
                     59:                for (j=i+1;j<=len;j++)
                     60:                    if (!attr[j-i]) return &data[j];
                     61:                    else {
                     62:                        if (j==len) break;
                     63:                        if (attr[j-i]!=tolower(data[j])) break;
                     64:                    }
                     65:            }
                     66:     return NULL;
                     67: }
                     68: 
                     69: // VForm
                     70: 
                     71: char *VForm::strpart(const char *str, int len) {
1.7       paf        72:     char *result=(char *)malloc(len+1);
1.3       paf        73:     if (!result) return NULL;
                     74:     memcpy(result,str,len);
                     75:     result[len]=0;
                     76:     return result;
                     77: }
                     78: 
                     79: char *VForm::getAttributeValue(const char *data,char *attr,int len) {
                     80:     const char *value=searchAttribute(data,attr,len);
                     81:     if (value){
                     82:        int i;
                     83:        if (!(len-=value-data)) return NULL;
                     84:        if (*value=='"') {
                     85:            for (i=1;i<len;i++) if (value[i]=='"') break;
                     86:            return strpart(&value[1],i-1);
                     87:        } else {
                     88:            for (i=0;i<len;i++) if (strchr(" ;\"\n\r",value[i])) break;
                     89:            return strpart(value,i);
                     90:        }
                     91:     }
                     92:     return NULL;
                     93: }
                     94: 
                     95: void VForm::ParseGetFormInput(const char *query_string) {
                     96:        ParseFormInput(query_string, strlen(query_string));
                     97: }
                     98: 
                     99: void VForm::ParsePostFormInput(const char *content_type, int post_size, 
                    100:                                                           bool mime_mode) {
                    101:        char *input;
                    102:        if(!post_size) 
                    103:                return;
                    104: 
                    105:        input=(char *) malloc(post_size);
                    106:        int read_size=(*service_funcs.read_post)(input, post_size);
                    107:        if(read_size !=post_size)
                    108:                THROW(0, 0,
                    109:                        0,
                    110:                        "ParsePostFormInput: post_size(%d)!=read_size(%d)", 
                    111:                                post_size, read_size);
                    112: 
                    113:        if(mime_mode)
                    114:                ParseMimeInput(content_type, input, post_size);
                    115:        else
                    116:                ParseFormInput(input, post_size);
                    117: }
                    118: 
                    119: void VForm::ParseFormInput(const char *data, int length) {
1.8     ! paf       120:        /* Scan for pairs, unescaping and storing them as they are found. */
1.3       paf       121:        int pos=0;
                    122:        while(pos !=length) {
                    123:                int foundEq=0;
                    124:                int foundAmp=0;
                    125:                int start=pos;
                    126:                int len=0;
                    127:                while(pos !=length) {
                    128:                        if(data[pos]=='=') {
                    129:                                foundEq=1;
                    130:                                pos++;
                    131:                                break;
                    132:                        }
                    133:                        pos++;
                    134:                        len++;
                    135:                }
                    136:                if(!foundEq)
                    137:                        break;
1.6       paf       138:                const char *attr=unescape_chars(pool(), data+start, len);
1.3       paf       139:                start=pos;
                    140:                len=0;
                    141:                while(pos !=length) {
                    142:                        if(data[pos]=='&') {
                    143:                                foundAmp=1;
                    144:                                pos++;
                    145:                                break;
                    146:                        }
                    147:                        pos++;
                    148:                        len++;
                    149:                }
                    150:                /* The last pair probably won't be followed by a &, but
                    151:                        that's fine, so check for that after accepting it */
1.6       paf       152:                const char *value=unescape_chars(pool(), data+start, len);
1.3       paf       153:                /* OK, we have a new pair, add it to the list. */
                    154:                AppendFormEntry(attr, value);
                    155:                if(!foundAmp)
                    156:                        break;
                    157:        }
                    158: }
                    159: 
                    160: void VForm::ParseMimeInput(const char *content_type, 
1.8     ! paf       161:                                                   const char *data, int length) {
1.3       paf       162: /* Scan for mime-presented pairs, storing them as they are found. */
                    163:        const char 
                    164:                *boundary=getAttributeValue(content_type,"boundary=",strlen(content_type)),
                    165:            *lastData=&data[length];
                    166:        if(!boundary) 
                    167:                THROW(0,0,
                    168:                        0,
                    169:                        "VForm::ParseMimeInput no boundary attribute of Content-Type");
                    170: 
                    171:        while(true) {
                    172:                const char 
                    173:                        *dataStart=searchAttribute(data,boundary,lastData-data),
                    174:                        *dataEnd=searchAttribute(dataStart,boundary,lastData-dataStart);
                    175:                int headerSize=getHeader(dataStart,lastData-dataStart);
                    176: 
                    177:                if(!dataStart|!dataEnd|!headerSize) break;
                    178:                if(searchAttribute(dataStart,"content-disposition: form-data",headerSize)){
                    179:                        int valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
                    180:                        char *attr=getAttributeValue(dataStart," name=",headerSize),
                    181:                             *fName=getAttributeValue(dataStart," filename=",headerSize);
                    182: 
                    183:                        if(attr && valueSize){
                    184:                                /* OK, we have a new pair, add it to the list. */
                    185:                                AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
                    186:                        }
                    187:                }
                    188:                data=(dataEnd-strlen(boundary));
                    189:        }
                    190: }
                    191: 
1.7       paf       192: void VForm::AppendFormEntry(const char *aname, 
1.3       paf       193:                                                        const char *value_ptr, int value_size,
                    194:                                                        const char *file_name) {
1.7       paf       195:        String& sname=*NEW String(pool(), aname);
1.3       paf       196: 
                    197:        Value *value;
                    198:        if(file_name)
                    199:                value=0; //TODO NEW VFile(...)
                    200:        else {
                    201:                String& string=*NEW String(pool());
                    202:                string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
                    203:                value=NEW VString(string);
                    204:        }
                    205: 
                    206:        fields.put(sname, value);
                    207: }
1.1       paf       208: 
1.6       paf       209: void VForm::fill_fields(Request& request, int post_max_size) {
1.3       paf       210:        // parsing QS [GET and ?name=value from uri rewrite)]
                    211:        if(request.info.query_string)
                    212:                ParseGetFormInput(request.info.query_string);
                    213:        // parsing POSTed data
1.4       paf       214:        if(request.info.method) {
1.3       paf       215:                if(const char *content_type=request.info.content_type)
1.4       paf       216:                        if(StrEqNc(request.info.method, "post",true)) {
1.3       paf       217:                                int post_size=max(0, min(request.info.content_length, post_max_size));
                    218:                                if(StrEqNc(content_type, "application/x-www-form-urlencoded",true)) 
                    219:                                        ParsePostFormInput(content_type, post_size, false);
                    220:                                else if(StrEqNc(content_type, "multipart/form-data",0))
                    221:                                        ParsePostFormInput(content_type, post_size, true);
                    222:                        }
                    223:        } else
                    224:                ; // TODO: разобрать пришедшее письмо, если какой ключик выставлен?
1.1       paf       225: }

E-mail: