|
|
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.6 ! paf 6: $Id: pa_vform.C,v 1.5 2001/03/18 13:38:49 paf Exp $
1.1 paf 7: */
8:
1.3 paf 9: /*
10: based on cgic by TODO:?
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"
1.6 ! paf 19: //#include "pa_common.h"
1.3 paf 20: #include "pa_request.h"
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) {
73: char *result=(char *)malloc(len+1); // TODO
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: //free(input);
119: }
120:
121: void VForm::ParseFormInput(const char *data, int length) {
122: /* Scan for pairs, unescaping and storing them as they are found. */
123: int pos=0;
124: while(pos !=length) {
125: int foundEq=0;
126: int foundAmp=0;
127: int start=pos;
128: int len=0;
129: while(pos !=length) {
130: if(data[pos]=='=') {
131: foundEq=1;
132: pos++;
133: break;
134: }
135: pos++;
136: len++;
137: }
138: if(!foundEq)
139: break;
1.6 ! paf 140: const char *attr=unescape_chars(pool(), data+start, len);
1.3 paf 141: start=pos;
142: len=0;
143: while(pos !=length) {
144: if(data[pos]=='&') {
145: foundAmp=1;
146: pos++;
147: break;
148: }
149: pos++;
150: len++;
151: }
152: /* The last pair probably won't be followed by a &, but
153: that's fine, so check for that after accepting it */
1.6 ! paf 154: const char *value=unescape_chars(pool(), data+start, len);
1.3 paf 155: /* OK, we have a new pair, add it to the list. */
156: AppendFormEntry(attr, value);
157: if(!foundAmp)
158: break;
159: }
160: }
161:
162: void VForm::ParseMimeInput(const char *content_type,
163: const char *data, int length) {
164: /* Scan for mime-presented pairs, storing them as they are found. */
165: const char
166: *boundary=getAttributeValue(content_type,"boundary=",strlen(content_type)),
167: *lastData=&data[length];
168: if(!boundary)
169: THROW(0,0,
170: 0,
171: "VForm::ParseMimeInput no boundary attribute of Content-Type");
172:
173: while(true) {
174: const char
175: *dataStart=searchAttribute(data,boundary,lastData-data),
176: *dataEnd=searchAttribute(dataStart,boundary,lastData-dataStart);
177: int headerSize=getHeader(dataStart,lastData-dataStart);
178:
179: if(!dataStart|!dataEnd|!headerSize) break;
180: if(searchAttribute(dataStart,"content-disposition: form-data",headerSize)){
181: int valueSize=(dataEnd-dataStart)-headerSize-5-strlen(boundary);
182: char *attr=getAttributeValue(dataStart," name=",headerSize),
183: *fName=getAttributeValue(dataStart," filename=",headerSize);
184:
185: if(attr && valueSize){
186: /* OK, we have a new pair, add it to the list. */
187: AppendFormEntry(attr, &dataStart[headerSize+1], valueSize, fName);
188: }
189: }
190: data=(dataEnd-strlen(boundary));
191: }
192: }
193:
194: void VForm::AppendFormEntry(const char *name,
195: const char *value_ptr, int value_size,
196: const char *file_name) {
1.5 paf 197: String& sname=*NEW String(pool(), name);
1.3 paf 198:
199: Value *value;
200: if(file_name)
201: value=0; //TODO NEW VFile(...)
202: else {
203: String& string=*NEW String(pool());
204: string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
205: value=NEW VString(string);
206: }
207:
208: fields.put(sname, value);
209: }
1.1 paf 210:
1.6 ! paf 211: void VForm::fill_fields(Request& request, int post_max_size) {
1.3 paf 212: // parsing QS [GET and ?name=value from uri rewrite)]
213: if(request.info.query_string)
214: ParseGetFormInput(request.info.query_string);
215: // parsing POSTed data
1.4 paf 216: if(request.info.method) {
1.3 paf 217: if(const char *content_type=request.info.content_type)
1.4 paf 218: if(StrEqNc(request.info.method, "post",true)) {
1.3 paf 219: int post_size=max(0, min(request.info.content_length, post_max_size));
220: if(StrEqNc(content_type, "application/x-www-form-urlencoded",true))
221: ParsePostFormInput(content_type, post_size, false);
222: else if(StrEqNc(content_type, "multipart/form-data",0))
223: ParsePostFormInput(content_type, post_size, true);
224: }
225: } else
226: ; // TODO: разобрать пришедшее письмо, если какой ключик выставлен?
1.1 paf 227: }