Annotation of parser3/src/types/pa_vform.C, revision 1.24.2.1
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.24.2.1! paf 8: $Id: pa_vform.C,v 1.24 2001/04/11 08:13:43 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){
1.24.2.1! paf 28: size_t i;
! 29: int enter=-1;
1.3 paf 30: if (data)
31: for (i=0;i<len;i++)
32: if (data[i]=='\n'){
33: if (enter>=0) enter++;
34: if (enter>1) return i;
35: } else if (data[i]!='\r') enter=0;
36: return 0;
37: }
38:
1.18 paf 39: static const char *searchAttribute(const char *data, const char *attr, size_t len){
1.16 paf 40: size_t i;
1.3 paf 41: if (data)
42: for (i=0;i<len;i++)
43: if (tolower(data[i])==*attr){
1.16 paf 44: size_t j;
1.3 paf 45: for (j=i+1;j<=len;j++)
46: if (!attr[j-i]) return &data[j];
47: else {
48: if (j==len) break;
49: if (attr[j-i]!=tolower(data[j])) break;
50: }
51: }
52: return NULL;
53: }
54:
55: // VForm
56:
1.16 paf 57: char *VForm::strpart(const char *str, size_t len) {
1.7 paf 58: char *result=(char *)malloc(len+1);
1.3 paf 59: if (!result) return NULL;
1.18 paf 60: memcpy(result, str, len);
1.3 paf 61: result[len]=0;
62: return result;
63: }
64:
1.18 paf 65: char *VForm::getAttributeValue(const char *data, char *attr, size_t len) {
66: const char *value=searchAttribute(data, attr, len);
1.3 paf 67: if (value){
1.18 paf 68: size_t i;
69: if (!(len-=value-data)) return NULL;
70: if (*value=='"') {
71: for (i=1;i<len;i++) if (value[i]=='"') break;
72: return strpart(&value[1], i-1);
73: } else {
74: for (i=0;i<len;i++) if (strchr(" ;\"\n\r", value[i])) break;
75: return strpart(value, i);
76: }
1.3 paf 77: }
78: return NULL;
79: }
80:
81: void VForm::ParseGetFormInput(const char *query_string) {
82: ParseFormInput(query_string, strlen(query_string));
83: }
84:
1.16 paf 85: void VForm::ParseFormInput(const char *data, size_t length) {
1.8 paf 86: /* Scan for pairs, unescaping and storing them as they are found. */
1.16 paf 87: size_t pos=0;
1.3 paf 88: while(pos !=length) {
1.16 paf 89: size_t foundEq=0;
90: size_t foundAmp=0;
91: size_t start=pos;
92: size_t len=0;
1.3 paf 93: while(pos !=length) {
94: if(data[pos]=='=') {
95: foundEq=1;
96: pos++;
97: break;
98: }
99: pos++;
100: len++;
101: }
102: if(!foundEq)
103: break;
1.6 paf 104: const char *attr=unescape_chars(pool(), data+start, len);
1.3 paf 105: start=pos;
106: len=0;
107: while(pos !=length) {
108: if(data[pos]=='&') {
109: foundAmp=1;
110: pos++;
111: break;
112: }
113: pos++;
114: len++;
115: }
116: /* The last pair probably won't be followed by a &, but
117: that's fine, so check for that after accepting it */
1.6 paf 118: const char *value=unescape_chars(pool(), data+start, len);
1.3 paf 119: /* OK, we have a new pair, add it to the list. */
120: AppendFormEntry(attr, value);
121: if(!foundAmp)
122: break;
123: }
124: }
125:
126: void VForm::ParseMimeInput(const char *content_type,
1.16 paf 127: const char *data, size_t length) {
1.3 paf 128: /* Scan for mime-presented pairs, storing them as they are found. */
129: const char
1.18 paf 130: *boundary=getAttributeValue(content_type, "boundary=", strlen(content_type)),
1.3 paf 131: *lastData=&data[length];
132: if(!boundary)
1.18 paf 133: THROW(0, 0,
134: 0,
1.3 paf 135: "VForm::ParseMimeInput no boundary attribute of Content-Type");
136:
137: while(true) {
138: const char
1.18 paf 139: *dataStart=searchAttribute(data, boundary, lastData-data),
140: *dataEnd=searchAttribute(dataStart, boundary, lastData-dataStart);
141: size_t headerSize=getHeader(dataStart, lastData-dataStart);
1.3 paf 142:
143: if(!dataStart|!dataEnd|!headerSize) break;
1.18 paf 144: if(searchAttribute(dataStart, "content-disposition: form-data", headerSize)){
1.16 paf 145: size_t valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
1.18 paf 146: char *attr=getAttributeValue(dataStart, " name=", headerSize),
147: *fName=getAttributeValue(dataStart, " filename=", headerSize);
1.3 paf 148:
149: if(attr && valueSize){
150: /* OK, we have a new pair, add it to the list. */
151: AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
152: }
153: }
154: data=(dataEnd-strlen(boundary));
155: }
156: }
157:
1.7 paf 158: void VForm::AppendFormEntry(const char *aname,
1.18 paf 159: const char *value_ptr, size_t value_size,
1.3 paf 160: const char *file_name) {
1.7 paf 161: String& sname=*NEW String(pool(), aname);
1.3 paf 162:
163: Value *value;
1.17 paf 164: if(file_name) {
165: VFile *vfile=NEW VFile(pool());
1.22 paf 166: vfile->set(true/*tainted*/, value_ptr, value_size, file_name);
1.17 paf 167: value=vfile;
168: } else {
1.3 paf 169: String& string=*NEW String(pool());
170: string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
171: value=NEW VString(string);
172: }
173:
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: