|
|
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.7 ! paf 6: $Id: pa_vform.C,v 1.6 2001/03/18 20:31:30 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"
20:
21: // parse helper funcs
22:
23: static bool StrEqNc(const char *s1, const char *s2, bool strict) {
24: while(true) {
25: if(!(*s1)) {
26: if(!(*s2))
27: return true;
28: else
29: return !strict;
30: } else if(!(*s2))
31: return !strict;
32: if(isalpha(*s1)) {
33: if(tolower(*s1) !=tolower(*s2))
34: return false;
35: } else if((*s1) !=(*s2))
36: return false;
37: s1++;
38: s2++;
39: }
40: }
41:
42: static int getHeader(const char *data,int len){
43: int i,enter=-1;
44: if (data)
45: for (i=0;i<len;i++)
46: if (data[i]=='\n'){
47: if (enter>=0) enter++;
48: if (enter>1) return i;
49: } else if (data[i]!='\r') enter=0;
50: return 0;
51: }
52:
53: static const char *searchAttribute(const char *data,const char *attr,int len){
54: int i;
55: if (data)
56: for (i=0;i<len;i++)
57: if (tolower(data[i])==*attr){
58: int j;
59: for (j=i+1;j<=len;j++)
60: if (!attr[j-i]) return &data[j];
61: else {
62: if (j==len) break;
63: if (attr[j-i]!=tolower(data[j])) break;
64: }
65: }
66: return NULL;
67: }
68:
69: // VForm
70:
71: char *VForm::strpart(const char *str, int len) {
1.7 ! paf 72: char *result=(char *)malloc(len+1);
1.3 paf 73: if (!result) return NULL;
74: memcpy(result,str,len);
75: result[len]=0;
76: return result;
77: }
78:
79: char *VForm::getAttributeValue(const char *data,char *attr,int len) {
80: const char *value=searchAttribute(data,attr,len);
81: if (value){
82: int i;
83: if (!(len-=value-data)) return NULL;
84: if (*value=='"') {
85: for (i=1;i<len;i++) if (value[i]=='"') break;
86: return strpart(&value[1],i-1);
87: } else {
88: for (i=0;i<len;i++) if (strchr(" ;\"\n\r",value[i])) break;
89: return strpart(value,i);
90: }
91: }
92: return NULL;
93: }
94:
95: void VForm::ParseGetFormInput(const char *query_string) {
96: ParseFormInput(query_string, strlen(query_string));
97: }
98:
99: void VForm::ParsePostFormInput(const char *content_type, int post_size,
100: bool mime_mode) {
101: char *input;
102: if(!post_size)
103: return;
104:
105: input=(char *) malloc(post_size);
106: int read_size=(*service_funcs.read_post)(input, post_size);
107: if(read_size !=post_size)
108: THROW(0, 0,
109: 0,
110: "ParsePostFormInput: post_size(%d)!=read_size(%d)",
111: post_size, read_size);
112:
113: if(mime_mode)
114: ParseMimeInput(content_type, input, post_size);
115: else
116: ParseFormInput(input, post_size);
117: //free(input);
118: }
119:
120: void VForm::ParseFormInput(const char *data, int length) {
121: /* Scan for pairs, unescaping and storing them as they are found. */
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,
162: const char *data, int length) {
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)
200: value=0; //TODO NEW VFile(...)
201: else {
202: String& string=*NEW String(pool());
203: string.APPEND_TAINTED(value_ptr, value_size, "form", 0);
204: value=NEW VString(string);
205: }
206:
207: fields.put(sname, value);
208: }
1.1 paf 209:
1.6 paf 210: void VForm::fill_fields(Request& request, int post_max_size) {
1.3 paf 211: // parsing QS [GET and ?name=value from uri rewrite)]
212: if(request.info.query_string)
213: ParseGetFormInput(request.info.query_string);
214: // parsing POSTed data
1.4 paf 215: if(request.info.method) {
1.3 paf 216: if(const char *content_type=request.info.content_type)
1.4 paf 217: if(StrEqNc(request.info.method, "post",true)) {
1.3 paf 218: int post_size=max(0, min(request.info.content_length, post_max_size));
219: if(StrEqNc(content_type, "application/x-www-form-urlencoded",true))
220: ParsePostFormInput(content_type, post_size, false);
221: else if(StrEqNc(content_type, "multipart/form-data",0))
222: ParsePostFormInput(content_type, post_size, true);
223: }
224: } else
225: ; // TODO: разобрать пришедшее письмо, если какой ключик выставлен?
1.1 paf 226: }