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

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

E-mail: