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

1.27      paf         1: /**    @file
                      2:        Parser: @b form class.
1.36      parser      3:        based on The CGI_C library, by Thomas Boutell.
1.10      paf         4: 
1.51      paf         5:        Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com)
1.52    ! paf         6:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.38      parser      7:        
1.52    ! paf         8:        $Id: pa_vform.C,v 1.51 2002/02/08 07:27:52 paf Exp $
1.39      parser      9: 
1.38      parser     10:        based on The CGI_C library, by Thomas Boutell.
1.3       paf        11: */
                     12: 
1.14      paf        13: #include "pa_sapi.h"
1.1       paf        14: #include "pa_vform.h"
                     15: #include "pa_vstring.h"
1.3       paf        16: #include "pa_globals.h"
                     17: #include "pa_request.h"
1.9       paf        18: #include "pa_vfile.h"
1.21      paf        19: #include "pa_common.h"
1.30      parser     20: #include "pa_vtable.h"
1.50      paf        21: #include "pa_charset.h"
1.3       paf        22: 
                     23: // parse helper funcs
                     24: 
1.18      paf        25: static size_t getHeader(const char *data, size_t len){
1.25      paf        26:     size_t i;
                     27:     int enter=-1;
1.3       paf        28:     if (data)
                     29:        for (i=0;i<len;i++)
                     30:            if (data[i]=='\n'){
                     31:                if (enter>=0) enter++;
                     32:                if (enter>1) return i;
                     33:            } else if (data[i]!='\r') enter=0;
                     34:     return 0;
                     35: }
                     36: 
1.42      parser     37: static char *searchAttribute(char *data, const char *attr, size_t len){
1.16      paf        38:     size_t i;
1.3       paf        39:     if (data)
                     40:        for (i=0;i<len;i++)
                     41:            if (tolower(data[i])==*attr){
1.16      paf        42:                size_t j;
1.3       paf        43:                for (j=i+1;j<=len;j++)
                     44:                    if (!attr[j-i]) return &data[j];
                     45:                    else {
                     46:                        if (j==len) break;
                     47:                        if (attr[j-i]!=tolower(data[j])) break;
                     48:                    }
                     49:            }
                     50:     return NULL;
                     51: }
                     52: 
                     53: // VForm
                     54: 
1.50      paf        55: VForm::VForm(Pool& apool) : VStateless_class(apool, form_base_class),
                     56:        fields(apool),
                     57:        tables(apool) {
                     58: }
                     59: 
1.16      paf        60: char *VForm::strpart(const char *str, size_t len) {
1.7       paf        61:     char *result=(char *)malloc(len+1);
1.3       paf        62:     if (!result) return NULL;
1.18      paf        63:     memcpy(result, str, len);
1.3       paf        64:     result[len]=0;
                     65:     return result;
                     66: }
                     67: 
1.42      parser     68: char *VForm::getAttributeValue(char *data, char *attr, size_t len) {
                     69:     char *value=searchAttribute(data, attr, len);
1.3       paf        70:     if (value){
1.18      paf        71:                size_t i;
                     72:                if (!(len-=value-data)) return NULL;
                     73:                if (*value=='"') {
                     74:                        for (i=1;i<len;i++)     if (value[i]=='"') break;
                     75:                        return strpart(&value[1], i-1);
                     76:                } else {
                     77:                        for (i=0;i<len;i++)     if (strchr(" ;\"\n\r", value[i])) break;
                     78:                        return strpart(value, i);
                     79:                }
1.3       paf        80:     }
                     81:     return NULL;
                     82: }
                     83: 
1.49      paf        84: void VForm::transcode(
                     85:        const void *client_body, size_t client_content_length,
                     86:        const void *& source_body, size_t& source_content_length) {
1.50      paf        87:        Charset::transcode(pool(),
                     88:                pool().get_client_charset(), client_body, client_content_length,
                     89:                pool().get_source_charset(), source_body, source_content_length);
1.49      paf        90: }
                     91: 
1.42      parser     92: void VForm::ParseGetFormInput(char *query_string, size_t length) {
                     93:        ParseFormInput(query_string, length);
1.3       paf        94: }
                     95: 
1.42      parser     96: void VForm::ParseFormInput(char *data, size_t length) {
1.8       paf        97:        /* Scan for pairs, unescaping and storing them as they are found. */
1.16      paf        98:        size_t pos=0;
1.3       paf        99:        while(pos !=length) {
1.16      paf       100:                size_t foundEq=0;
                    101:                size_t foundAmp=0;
                    102:                size_t start=pos;
                    103:                size_t len=0;
1.3       paf       104:                while(pos !=length) {
                    105:                        if(data[pos]=='=') {
                    106:                                foundEq=1;
                    107:                                pos++;
                    108:                                break;
                    109:                        }
                    110:                        pos++;
                    111:                        len++;
                    112:                }
                    113:                if(!foundEq)
                    114:                        break;
1.42      parser    115:                char *attr=unescape_chars(pool(), data+start, len);
1.3       paf       116:                start=pos;
                    117:                len=0;
                    118:                while(pos !=length) {
                    119:                        if(data[pos]=='&') {
                    120:                                foundAmp=1;
                    121:                                pos++;
                    122:                                break;
                    123:                        }
                    124:                        pos++;
                    125:                        len++;
                    126:                }
                    127:                /* The last pair probably won't be followed by a &, but
                    128:                        that's fine, so check for that after accepting it */
1.42      parser    129:                char *value=unescape_chars(pool(), data+start, len);
1.3       paf       130:                /* OK, we have a new pair, add it to the list. */
1.45      paf       131:                size_t value_size=strlen(value);
                    132:                AppendFormEntry(attr, value, value_size);
                    133: 
1.3       paf       134:                if(!foundAmp)
                    135:                        break;
                    136:        }
                    137: }
                    138: 
1.49      paf       139: void VForm::ParseMimeInput(
                    140:                                                   char *content_type, 
1.42      parser    141:                                                   char *data, size_t length) {
1.3       paf       142: /* Scan for mime-presented pairs, storing them as they are found. */
                    143:        const char 
1.18      paf       144:                *boundary=getAttributeValue(content_type, "boundary=", strlen(content_type)), 
1.3       paf       145:            *lastData=&data[length];
                    146:        if(!boundary) 
1.40      parser    147:                throw Exception(0, 0, 
1.18      paf       148:                        0, 
1.3       paf       149:                        "VForm::ParseMimeInput no boundary attribute of Content-Type");
                    150: 
                    151:        while(true) {
1.42      parser    152:                char 
1.18      paf       153:                        *dataStart=searchAttribute(data, boundary, lastData-data), 
                    154:                        *dataEnd=searchAttribute(dataStart, boundary, lastData-dataStart);
                    155:                size_t headerSize=getHeader(dataStart, lastData-dataStart);
1.3       paf       156: 
                    157:                if(!dataStart|!dataEnd|!headerSize) break;
1.47      paf       158:                if(searchAttribute(dataStart, "content-disposition: form-data", headerSize)) {
1.16      paf       159:                        size_t valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
1.18      paf       160:                        char *attr=getAttributeValue(dataStart, " name=", headerSize), 
                    161:                             *fName=getAttributeValue(dataStart, " filename=", headerSize);
1.3       paf       162: 
1.48      paf       163:                        if(attr && valueSize) {
1.3       paf       164:                                /* OK, we have a new pair, add it to the list. */
                    165:                                AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
                    166:                        }
                    167:                }
                    168:                data=(dataEnd-strlen(boundary));
                    169:        }
                    170: }
                    171: 
1.49      paf       172: void VForm::AppendFormEntry(
                    173:                                                        const char *cname_cstr, 
                    174:                                                        char *cvalue_ptr, size_t cvalue_size, 
1.3       paf       175:                                                        const char *file_name) {
1.49      paf       176:        const void *sname_ptr;
                    177:        size_t sname_size;
                    178:        transcode(
                    179:                cname_cstr, strlen(cname_cstr),
                    180:                sname_ptr, sname_size);
                    181:        String& sname=*NEW String(pool(), (const char *)sname_ptr, sname_size);
1.3       paf       182: 
                    183:        Value *value;
1.17      paf       184:        if(file_name) {
                    185:                VFile *vfile=NEW VFile(pool());
1.49      paf       186:                vfile->set(true/*tainted*/, cvalue_ptr, cvalue_size, file_name);
1.17      paf       187:                value=vfile;
                    188:        } else {
1.49      paf       189:                fix_line_breaks(cvalue_ptr, cvalue_size);
                    190: 
                    191:                const void *svalue_ptr; size_t svalue_size;
                    192:                transcode(
                    193:                        cvalue_ptr, cvalue_size, // client [from]
                    194:                        svalue_ptr, svalue_size); // source [to]
1.47      paf       195:                
1.3       paf       196:                String& string=*NEW String(pool());
1.49      paf       197:                string.APPEND_TAINTED((const char*)svalue_ptr, svalue_size, "form", 0);
1.36      parser    198: 
                    199:                // tables
                    200:                {
                    201:                        Value *valready=(Value *)tables.get(sname);
                    202:                        bool existed=valready!=0;
                    203:                        Table *table;
                    204:                        if(existed) {
                    205:                                // second+ appearence
                    206:                                table=valready->get_table();
                    207:                        } else {
                    208:                                // first appearence
1.30      parser    209:                                Array& columns=*NEW Array(pool(), 1);
1.36      parser    210:                                columns+=NEW String(pool(), "field");
1.30      parser    211:                                table=NEW Table(pool(), 0, &columns);
                    212:                        }
                    213:                        // this string becomes next row
                    214:                        Array& row=*NEW Array(pool(), 1);
                    215:                        row+=&string;
                    216:                        *table+=&row;
1.36      parser    217:                        // not existed before? add it
                    218:                        if(!existed)
                    219:                                tables.put(sname, NEW VTable(pool(), table));
                    220:                }
                    221:                value=NEW VString(string);
1.3       paf       222:        }
                    223: 
1.32      parser    224:        fields.put_dont_replace(sname, value);
1.3       paf       225: }
1.1       paf       226: 
1.26      paf       227: /// @todo parse input letter if some switch is on
1.36      parser    228: void VForm::fill_fields_and_tables(Request& request) {
1.3       paf       229:        // parsing QS [GET and ?name=value from uri rewrite)]
1.42      parser    230:        if(request.info.query_string) {
                    231:                size_t length=strlen(request.info.query_string);
                    232:                char *buf=(char *)malloc(length);
                    233:                memcpy(buf, request.info.query_string, length);
                    234:                ParseGetFormInput(buf, length);
                    235:        }
                    236:        
1.3       paf       237:        // parsing POSTed data
1.4       paf       238:        if(request.info.method) {
1.3       paf       239:                if(const char *content_type=request.info.content_type)
1.18      paf       240:                        if(StrEqNc(request.info.method, "post", true)) {
                    241:                                if(StrEqNc(content_type, "application/x-www-form-urlencoded", true)) 
1.21      paf       242:                                        ParseFormInput(request.post_data, request.post_size);
1.42      parser    243:                                else if(StrEqNc(content_type, "multipart/form-data", 0)) {
                    244:                                        size_t length=strlen(content_type)+1;
                    245:                                        char *buf=(char *)malloc(length);
                    246:                                        memcpy(buf, content_type, length);
                    247:                                        ParseMimeInput(buf, request.post_data, request.post_size);
                    248:                                }
1.3       paf       249:                        }
                    250:        } else
1.21      paf       251:                ; // letter?
1.1       paf       252: }
1.33      parser    253: 
                    254: Value *VForm::get_element(const String& aname) {
1.36      parser    255:        // $fields
                    256:        if(aname==FORM_FIELDS_ELEMENT_NAME)
                    257:                return NEW VHash(pool(), Hash(fields));
                    258: 
                    259:        // $tables
                    260:        if(aname==FORM_TABLES_ELEMENT_NAME)
                    261:                return NEW VHash(pool(), Hash(tables));
                    262: 
1.33      parser    263:        // $CLASS,$method
                    264:        if(Value *result=VStateless_class::get_element(aname))
                    265:                return result;
                    266: 
                    267:        // $element
                    268:        return static_cast<Value *>(fields.get(aname));
                    269: }
                    270: 

E-mail: