Annotation of parser3/src/types/pa_vform.C, revision 1.23
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.23 ! paf 8: $Id: pa_vform.C,v 1.22 2001/04/09 11:30:44 paf Exp $
1.1 paf 9:
1.15 paf 10:
11: based on The CGI_C library, by Thomas Boutell.
1.3 paf 12: */
13:
1.15 paf 14: #include "pa_config_includes.h"
1.3 paf 15: #include <ctype.h>
16:
1.14 paf 17: #include "pa_sapi.h"
1.1 paf 18: #include "pa_vform.h"
19: #include "pa_vstring.h"
1.3 paf 20: #include "pa_globals.h"
21: #include "pa_request.h"
1.9 paf 22: #include "pa_vfile.h"
1.21 paf 23: #include "pa_common.h"
1.3 paf 24:
25: // parse helper funcs
26:
1.18 paf 27: static size_t getHeader(const char *data, size_t len){
28: size_t i, enter=-1;
1.3 paf 29: if (data)
30: for (i=0;i<len;i++)
31: if (data[i]=='\n'){
32: if (enter>=0) enter++;
33: if (enter>1) return i;
34: } else if (data[i]!='\r') enter=0;
35: return 0;
36: }
37:
1.18 paf 38: static const char *searchAttribute(const char *data, const char *attr, size_t len){
1.16 paf 39: size_t i;
1.3 paf 40: if (data)
41: for (i=0;i<len;i++)
42: if (tolower(data[i])==*attr){
1.16 paf 43: size_t j;
1.3 paf 44: for (j=i+1;j<=len;j++)
45: if (!attr[j-i]) return &data[j];
46: else {
47: if (j==len) break;
48: if (attr[j-i]!=tolower(data[j])) break;
49: }
50: }
51: return NULL;
52: }
53:
54: // VForm
55:
1.16 paf 56: char *VForm::strpart(const char *str, size_t len) {
1.7 paf 57: char *result=(char *)malloc(len+1);
1.3 paf 58: if (!result) return NULL;
1.18 paf 59: memcpy(result, str, len);
1.3 paf 60: result[len]=0;
61: return result;
62: }
63:
1.18 paf 64: char *VForm::getAttributeValue(const char *data, char *attr, size_t len) {
65: const char *value=searchAttribute(data, attr, len);
1.3 paf 66: if (value){
1.18 paf 67: size_t i;
68: if (!(len-=value-data)) return NULL;
69: if (*value=='"') {
70: for (i=1;i<len;i++) if (value[i]=='"') break;
71: return strpart(&value[1], i-1);
72: } else {
73: for (i=0;i<len;i++) if (strchr(" ;\"\n\r", value[i])) break;
74: return strpart(value, i);
75: }
1.3 paf 76: }
77: return NULL;
78: }
79:
80: void VForm::ParseGetFormInput(const char *query_string) {
81: ParseFormInput(query_string, strlen(query_string));
82: }
83:
1.16 paf 84: void VForm::ParseFormInput(const char *data, size_t length) {
1.8 paf 85: /* Scan for pairs, unescaping and storing them as they are found. */
1.16 paf 86: size_t pos=0;
1.3 paf 87: while(pos !=length) {
1.16 paf 88: size_t foundEq=0;
89: size_t foundAmp=0;
90: size_t start=pos;
91: size_t len=0;
1.3 paf 92: while(pos !=length) {
93: if(data[pos]=='=') {
94: foundEq=1;
95: pos++;
96: break;
97: }
98: pos++;
99: len++;
100: }
101: if(!foundEq)
102: break;
1.6 paf 103: const char *attr=unescape_chars(pool(), data+start, len);
1.3 paf 104: start=pos;
105: len=0;
106: while(pos !=length) {
107: if(data[pos]=='&') {
108: foundAmp=1;
109: pos++;
110: break;
111: }
112: pos++;
113: len++;
114: }
115: /* The last pair probably won't be followed by a &, but
116: that's fine, so check for that after accepting it */
1.6 paf 117: const char *value=unescape_chars(pool(), data+start, len);
1.3 paf 118: /* OK, we have a new pair, add it to the list. */
119: AppendFormEntry(attr, value);
120: if(!foundAmp)
121: break;
122: }
123: }
124:
125: void VForm::ParseMimeInput(const char *content_type,
1.16 paf 126: const char *data, size_t length) {
1.3 paf 127: /* Scan for mime-presented pairs, storing them as they are found. */
128: const char
1.18 paf 129: *boundary=getAttributeValue(content_type, "boundary=", strlen(content_type)),
1.3 paf 130: *lastData=&data[length];
131: if(!boundary)
1.18 paf 132: THROW(0, 0,
133: 0,
1.3 paf 134: "VForm::ParseMimeInput no boundary attribute of Content-Type");
135:
136: while(true) {
137: const char
1.18 paf 138: *dataStart=searchAttribute(data, boundary, lastData-data),
139: *dataEnd=searchAttribute(dataStart, boundary, lastData-dataStart);
140: size_t headerSize=getHeader(dataStart, lastData-dataStart);
1.3 paf 141:
142: if(!dataStart|!dataEnd|!headerSize) break;
1.18 paf 143: if(searchAttribute(dataStart, "content-disposition: form-data", headerSize)){
1.16 paf 144: size_t valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
1.18 paf 145: char *attr=getAttributeValue(dataStart, " name=", headerSize),
146: *fName=getAttributeValue(dataStart, " filename=", headerSize);
1.3 paf 147:
148: if(attr && valueSize){
149: /* OK, we have a new pair, add it to the list. */
150: AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
151: }
152: }
153: data=(dataEnd-strlen(boundary));
154: }
155: }
156:
1.7 paf 157: void VForm::AppendFormEntry(const char *aname,
1.18 paf 158: const char *value_ptr, size_t value_size,
1.3 paf 159: const char *file_name) {
1.7 paf 160: String& sname=*NEW String(pool(), aname);
1.3 paf 161:
162: Value *value;
1.17 paf 163: if(file_name) {
164: VFile *vfile=NEW VFile(pool());
1.22 paf 165: vfile->set(true/*tainted*/, value_ptr, value_size, file_name);
1.17 paf 166: value=vfile;
167: } else {
1.3 paf 168: String& string=*NEW String(pool());
169: string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
170: value=NEW VString(string);
171: }
172:
1.23 ! paf 173: SAPI::log(pool(), "AppendFormEntry %s", sname.cstr());
1.3 paf 174: fields.put(sname, value);
175: }
1.1 paf 176:
1.21 paf 177: /// @test parse input letter if some switch is on
178: void VForm::fill_fields(Request& request) {
1.20 paf 179: //AppendFormEntry("fs", "<1!2>", 5, 0);
1.3 paf 180: // parsing QS [GET and ?name=value from uri rewrite)]
181: if(request.info.query_string)
182: ParseGetFormInput(request.info.query_string);
183: // parsing POSTed data
1.4 paf 184: if(request.info.method) {
1.3 paf 185: if(const char *content_type=request.info.content_type)
1.18 paf 186: if(StrEqNc(request.info.method, "post", true)) {
187: if(StrEqNc(content_type, "application/x-www-form-urlencoded", true))
1.21 paf 188: ParseFormInput(request.post_data, request.post_size);
1.18 paf 189: else if(StrEqNc(content_type, "multipart/form-data", 0))
1.21 paf 190: ParseMimeInput(content_type, request.post_data, request.post_size);
1.3 paf 191: }
192: } else
1.21 paf 193: ; // letter?
1.1 paf 194: }
E-mail: