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

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

E-mail: