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

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

E-mail: