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