|
|
1.1 paf 1: /*
2: Parser
1.3 paf 3: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1 paf 5:
1.9 ! paf 6: $Id: pa_vform.C,v 1.8 2001/03/19 19:17:47 paf Exp $
1.1 paf 7: */
8:
1.7 paf 9: /**
10: \todo based on cgic by ?
1.3 paf 11: */
12:
13: #include <ctype.h>
14: #include <string.h>
15:
1.1 paf 16: #include "pa_vform.h"
17: #include "pa_vstring.h"
1.3 paf 18: #include "pa_globals.h"
19: #include "pa_request.h"
1.9 ! paf 20: #include "pa_vfile.h"
1.3 paf 21:
22: // parse helper funcs
23:
24: static bool StrEqNc(const char *s1, const char *s2, bool strict) {
25: while(true) {
26: if(!(*s1)) {
27: if(!(*s2))
28: return true;
29: else
30: return !strict;
31: } else if(!(*s2))
32: return !strict;
33: if(isalpha(*s1)) {
34: if(tolower(*s1) !=tolower(*s2))
35: return false;
36: } else if((*s1) !=(*s2))
37: return false;
38: s1++;
39: s2++;
40: }
41: }
42:
43: static int getHeader(const char *data,int len){
44: int i,enter=-1;
45: if (data)
46: for (i=0;i<len;i++)
47: if (data[i]=='\n'){
48: if (enter>=0) enter++;
49: if (enter>1) return i;
50: } else if (data[i]!='\r') enter=0;
51: return 0;
52: }
53:
54: static const char *searchAttribute(const char *data,const char *attr,int len){
55: int i;
56: if (data)
57: for (i=0;i<len;i++)
58: if (tolower(data[i])==*attr){
59: int j;
60: for (j=i+1;j<=len;j++)
61: if (!attr[j-i]) return &data[j];
62: else {
63: if (j==len) break;
64: if (attr[j-i]!=tolower(data[j])) break;
65: }
66: }
67: return NULL;
68: }
69:
70: // VForm
71:
72: char *VForm::strpart(const char *str, int len) {
1.7 paf 73: char *result=(char *)malloc(len+1);
1.3 paf 74: if (!result) return NULL;
75: memcpy(result,str,len);
76: result[len]=0;
77: return result;
78: }
79:
80: char *VForm::getAttributeValue(const char *data,char *attr,int len) {
81: const char *value=searchAttribute(data,attr,len);
82: if (value){
83: int i;
84: if (!(len-=value-data)) return NULL;
85: if (*value=='"') {
86: for (i=1;i<len;i++) if (value[i]=='"') break;
87: return strpart(&value[1],i-1);
88: } else {
89: for (i=0;i<len;i++) if (strchr(" ;\"\n\r",value[i])) break;
90: return strpart(value,i);
91: }
92: }
93: return NULL;
94: }
95:
96: void VForm::ParseGetFormInput(const char *query_string) {
97: ParseFormInput(query_string, strlen(query_string));
98: }
99:
100: void VForm::ParsePostFormInput(const char *content_type, int post_size,
101: bool mime_mode) {
102: char *input;
103: if(!post_size)
104: return;
105:
106: input=(char *) malloc(post_size);
107: int read_size=(*service_funcs.read_post)(input, post_size);
108: if(read_size !=post_size)
109: THROW(0, 0,
110: 0,
111: "ParsePostFormInput: post_size(%d)!=read_size(%d)",
112: post_size, read_size);
113:
114: if(mime_mode)
115: ParseMimeInput(content_type, input, post_size);
116: else
117: ParseFormInput(input, post_size);
118: }
119:
120: void VForm::ParseFormInput(const char *data, int length) {
1.8 paf 121: /* Scan for pairs, unescaping and storing them as they are found. */
1.3 paf 122: int pos=0;
123: while(pos !=length) {
124: int foundEq=0;
125: int foundAmp=0;
126: int start=pos;
127: int len=0;
128: while(pos !=length) {
129: if(data[pos]=='=') {
130: foundEq=1;
131: pos++;
132: break;
133: }
134: pos++;
135: len++;
136: }
137: if(!foundEq)
138: break;
1.6 paf 139: const char *attr=unescape_chars(pool(), data+start, len);
1.3 paf 140: start=pos;
141: len=0;
142: while(pos !=length) {
143: if(data[pos]=='&') {
144: foundAmp=1;
145: pos++;
146: break;
147: }
148: pos++;
149: len++;
150: }
151: /* The last pair probably won't be followed by a &, but
152: that's fine, so check for that after accepting it */
1.6 paf 153: const char *value=unescape_chars(pool(), data+start, len);
1.3 paf 154: /* OK, we have a new pair, add it to the list. */
155: AppendFormEntry(attr, value);
156: if(!foundAmp)
157: break;
158: }
159: }
160:
161: void VForm::ParseMimeInput(const char *content_type,
1.8 paf 162: const char *data, int length) {
1.3 paf 163: /* Scan for mime-presented pairs, storing them as they are found. */
164: const char
165: *boundary=getAttributeValue(content_type,"boundary=",strlen(content_type)),
166: *lastData=&data[length];
167: if(!boundary)
168: THROW(0,0,
169: 0,
170: "VForm::ParseMimeInput no boundary attribute of Content-Type");
171:
172: while(true) {
173: const char
174: *dataStart=searchAttribute(data,boundary,lastData-data),
175: *dataEnd=searchAttribute(dataStart,boundary,lastData-dataStart);
176: int headerSize=getHeader(dataStart,lastData-dataStart);
177:
178: if(!dataStart|!dataEnd|!headerSize) break;
179: if(searchAttribute(dataStart,"content-disposition: form-data",headerSize)){
180: int valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
181: char *attr=getAttributeValue(dataStart," name=",headerSize),
182: *fName=getAttributeValue(dataStart," filename=",headerSize);
183:
184: if(attr && valueSize){
185: /* OK, we have a new pair, add it to the list. */
186: AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
187: }
188: }
189: data=(dataEnd-strlen(boundary));
190: }
191: }
192:
1.7 paf 193: void VForm::AppendFormEntry(const char *aname,
1.3 paf 194: const char *value_ptr, int value_size,
195: const char *file_name) {
1.7 paf 196: String& sname=*NEW String(pool(), aname);
1.3 paf 197:
198: Value *value;
199: if(file_name)
1.9 ! paf 200: value=NEW VFile(pool(),
! 201: value_ptr, value_size,
! 202: file_name);
1.3 paf 203: else {
204: String& string=*NEW String(pool());
205: string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
206: value=NEW VString(string);
207: }
208:
209: fields.put(sname, value);
210: }
1.1 paf 211:
1.6 paf 212: void VForm::fill_fields(Request& request, int post_max_size) {
1.3 paf 213: // parsing QS [GET and ?name=value from uri rewrite)]
214: if(request.info.query_string)
215: ParseGetFormInput(request.info.query_string);
216: // parsing POSTed data
1.4 paf 217: if(request.info.method) {
1.3 paf 218: if(const char *content_type=request.info.content_type)
1.4 paf 219: if(StrEqNc(request.info.method, "post",true)) {
1.3 paf 220: int post_size=max(0, min(request.info.content_length, post_max_size));
221: if(StrEqNc(content_type, "application/x-www-form-urlencoded",true))
222: ParsePostFormInput(content_type, post_size, false);
223: else if(StrEqNc(content_type, "multipart/form-data",0))
224: ParsePostFormInput(content_type, post_size, true);
225: }
226: } else
227: ; // TODO: разобрать пришедшее письмо, если какой ключик выставлен?
1.1 paf 228: }