diff options
Diffstat (limited to 'src/clientinfo.c')
-rw-r--r-- | src/clientinfo.c | 1124 |
1 files changed, 1124 insertions, 0 deletions
diff --git a/src/clientinfo.c b/src/clientinfo.c new file mode 100644 index 00000000..c640973c --- /dev/null +++ b/src/clientinfo.c | |||
@@ -0,0 +1,1124 @@ | |||
1 | /* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved. | ||
2 | * | ||
3 | * See the LICENSE file | ||
4 | * | ||
5 | * The origin of this software must not be misrepresented, either by | ||
6 | * explicit claim or by omission. Since few users ever read sources, | ||
7 | * credits must appear in the documentation. | ||
8 | * | ||
9 | * date: Wed Oct 9 19:56:22 GMT 2002 | ||
10 | * | ||
11 | * -- parse http header into "ClientInfo" | ||
12 | * | ||
13 | */ | ||
14 | |||
15 | #include "clientinfo.h" | ||
16 | |||
17 | |||
18 | struct ClientInfo *ClientInfo; // tochange | ||
19 | |||
20 | /*********************************************************************************************************/ | ||
21 | /* | ||
22 | * Initialize ClientInfo structure | ||
23 | */ | ||
24 | void __ILWS_init_clientinfo() { | ||
25 | char *t; | ||
26 | struct outstream *tstream=current_web_client->outstream; | ||
27 | |||
28 | ClientInfo=__ILWS_malloc(sizeof(struct ClientInfo)); | ||
29 | if(ClientInfo==NULL) { | ||
30 | LWSERR(LE_MEMORY); | ||
31 | return; | ||
32 | }; | ||
33 | |||
34 | while(tstream->next!=NULL) { | ||
35 | tstream=tstream->next; | ||
36 | }; | ||
37 | |||
38 | if(tstream->fstream!=NULL) ClientInfo->outfd=fileno(tstream->fstream); //take it off? | ||
39 | |||
40 | ClientInfo->mem=__ILWS_init_buffer_list(); // First thing, other fuctions use this to allocate | ||
41 | |||
42 | |||
43 | ClientInfo->request=__ILWS_clientinfo_getreqname(); | ||
44 | |||
45 | ClientInfo->inetname=NULL; | ||
46 | t=inet_ntoa(current_web_client->sa.sin_addr); | ||
47 | if((ClientInfo->inetname=__ILWS_add_buffer(ClientInfo->mem,strlen(t)+1))) { | ||
48 | memcpy(ClientInfo->inetname,t,strlen(t)); | ||
49 | ClientInfo->inetname[strlen(t)]='\0'; | ||
50 | }; | ||
51 | |||
52 | ClientInfo->method=__ILWS_clientinfo_getmethod(); | ||
53 | ClientInfo->user=__ILWS_clientinfo_getauthuser(); | ||
54 | ClientInfo->pass=__ILWS_clientinfo_getauthpass(); | ||
55 | |||
56 | |||
57 | |||
58 | /* Initialize List's */ | ||
59 | ClientInfo->HeaderList=NULL; | ||
60 | ClientInfo->QueryList=NULL; | ||
61 | ClientInfo->PostList=NULL; | ||
62 | ClientInfo->MultiPartList=NULL; | ||
63 | ClientInfo->CookieList=NULL; | ||
64 | |||
65 | ClientInfo->Header=__ILWS_Header; | ||
66 | ClientInfo->Query=__ILWS_Query; | ||
67 | ClientInfo->QueryString=__ILWS_clientinfo_getquerystring(); | ||
68 | ClientInfo->Post=__ILWS_Post; | ||
69 | ClientInfo->PostData=__ILWS_clientinfo_getpostdata(); | ||
70 | ClientInfo->MultiPart=__ILWS_MultiPart; | ||
71 | ClientInfo->Cookie=__ILWS_Cookie; | ||
72 | ClientInfo->Conf=__ILWS_Conf; | ||
73 | ClientInfo->CookieString=__ILWS_Header("Cookie"); | ||
74 | |||
75 | } | ||
76 | |||
77 | /*********************************************************************************************************/ | ||
78 | /* | ||
79 | * Free ClientInfo structure | ||
80 | */ | ||
81 | void __ILWS_free_clientinfo() { | ||
82 | if(ClientInfo==NULL) { | ||
83 | return; | ||
84 | }; | ||
85 | __ILWS_delete_buffer_list(ClientInfo->mem); | ||
86 | |||
87 | __ILWS_free(ClientInfo); | ||
88 | ClientInfo=NULL; | ||
89 | } | ||
90 | |||
91 | |||
92 | /*********************************************************************************************************/ | ||
93 | /* | ||
94 | * Header function for ClientInfo->Header("x") | ||
95 | */ | ||
96 | char *__ILWS_Header(char *str) { | ||
97 | char *tmp1,*tmp2,*tmp3,*ret; | ||
98 | struct _Header *hl=ClientInfo->HeaderList; | ||
99 | char *defret=""; | ||
100 | size_t size; | ||
101 | size_t strsize; | ||
102 | if(str==NULL) { // request is null return whole header | ||
103 | return current_web_client->rbuf; | ||
104 | }; | ||
105 | if(ClientInfo->HeaderList==NULL) { | ||
106 | |||
107 | ClientInfo->HeaderList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Header)); | ||
108 | if(ClientInfo->HeaderList==NULL) { | ||
109 | LWSERR(LE_MEMORY); | ||
110 | return defret; | ||
111 | }; | ||
112 | ClientInfo->HeaderList->next=NULL; | ||
113 | ClientInfo->HeaderList->data=NULL; | ||
114 | ClientInfo->HeaderList->id=NULL; | ||
115 | hl=ClientInfo->HeaderList; | ||
116 | }; | ||
117 | // First search if exists | ||
118 | |||
119 | while(hl->next!=NULL) { | ||
120 | if(hl->next->id!=NULL) { | ||
121 | if(!strcmp(hl->next->id,str)) { | ||
122 | |||
123 | return hl->next->data; | ||
124 | }; | ||
125 | }; | ||
126 | hl=hl->next; | ||
127 | }; | ||
128 | |||
129 | /* Doesn't exists */ | ||
130 | strsize=strlen(str); | ||
131 | if(!(hl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Header)))) { | ||
132 | LWSERR(LE_MEMORY); | ||
133 | return defret; | ||
134 | }; | ||
135 | if(!(hl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1))) { | ||
136 | LWSERR(LE_MEMORY); | ||
137 | return defret; | ||
138 | }; | ||
139 | |||
140 | memcpy(hl->next->id,str,strsize); | ||
141 | hl->next->id[strsize]=0; | ||
142 | hl->next->data=defret; | ||
143 | hl->next->next=NULL; | ||
144 | |||
145 | if(!(tmp3=__ILWS_malloc(strsize+3))) { | ||
146 | LWSERR(LE_MEMORY); | ||
147 | return defret; | ||
148 | }; | ||
149 | snprintf(tmp3,strsize+3,"%s: ",str); | ||
150 | tmp1=__ILWS_stristr(current_web_client->rbuf,tmp3); | ||
151 | __ILWS_free(tmp3); | ||
152 | if(tmp1==NULL) { | ||
153 | return defret; | ||
154 | }; | ||
155 | |||
156 | tmp1+=strsize+2; | ||
157 | if(!(tmp2=strstr(tmp1,"\r\n"))) { // Unexpected (security anyway) | ||
158 | return defret; | ||
159 | }; | ||
160 | if((size=(unsigned int)(tmp2-tmp1))<0) { | ||
161 | return defret; | ||
162 | }; | ||
163 | if(!(ret=__ILWS_add_buffer(ClientInfo->mem,size+1))) { //malloc & register | ||
164 | return defret; | ||
165 | }; | ||
166 | memcpy(ret,tmp1,size); | ||
167 | ret[size]=0; | ||
168 | hl->next->data=ret; | ||
169 | return ret; | ||
170 | } | ||
171 | |||
172 | |||
173 | |||
174 | /*********************************************************************************************************/ | ||
175 | /* | ||
176 | * Function for Querydata | ||
177 | */ | ||
178 | char *__ILWS_Query(char *handle) { | ||
179 | char *tmp1,*tmp2,*tmp3,*tmp4,*ret; | ||
180 | char *defret=""; | ||
181 | size_t strsize; | ||
182 | size_t size; | ||
183 | int j=0,ch; | ||
184 | int seek=1; | ||
185 | unsigned int i; | ||
186 | unsigned int *iddb=NULL; | ||
187 | unsigned int *iddb2=NULL; | ||
188 | unsigned int idf=0; | ||
189 | int rw=0; // 0 data 1 number of vars; (return what?) | ||
190 | struct _Query *ql=ClientInfo->QueryList; | ||
191 | |||
192 | |||
193 | if(handle==NULL) { | ||
194 | return ClientInfo->QueryString; | ||
195 | }; | ||
196 | if(handle[0]=='#') rw=1; | ||
197 | // allocate first node from the list | ||
198 | if(ClientInfo->QueryList==NULL) { | ||
199 | ClientInfo->QueryList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Query)); | ||
200 | if(ClientInfo->QueryList==NULL) { | ||
201 | LWSERR(LE_MEMORY); | ||
202 | if(rw) return 0; | ||
203 | return defret; | ||
204 | }; | ||
205 | ClientInfo->QueryList->next=NULL; | ||
206 | ClientInfo->QueryList->data=NULL; | ||
207 | ClientInfo->QueryList->id=NULL; | ||
208 | ql=ClientInfo->QueryList; | ||
209 | }; | ||
210 | // done allocating | ||
211 | |||
212 | |||
213 | // First search if exists and fetch values; | ||
214 | |||
215 | idf=1; | ||
216 | iddb=&idf; | ||
217 | seek=1; | ||
218 | |||
219 | while(ql->next!=NULL) { | ||
220 | if(ql->next->id!=NULL) { | ||
221 | if(!strcmp(ql->next->id,handle+rw) && *iddb >= 0) { | ||
222 | if(seek==1) { | ||
223 | iddb=&ql->next->index; // atribute iddb to first node | ||
224 | iddb2=&ql->next->idf; // atribute iddb2 to counting | ||
225 | if(rw) return (char *)*iddb2; | ||
226 | if(ql->next->idf==1) { | ||
227 | return ql->next->data; | ||
228 | }; | ||
229 | j=*iddb; | ||
230 | seek++; | ||
231 | }; | ||
232 | *iddb=*iddb-1; | ||
233 | |||
234 | if(*iddb<=0) { | ||
235 | *iddb=j-1; | ||
236 | if(j<=1) { | ||
237 | *iddb=*iddb2; // go to start if any | ||
238 | //return defret; // to be null | ||
239 | }; | ||
240 | return ql->next->data; // Return existent | ||
241 | }; | ||
242 | |||
243 | }; | ||
244 | }; | ||
245 | ql=ql->next; | ||
246 | }; | ||
247 | |||
248 | |||
249 | |||
250 | /* Doesn't exists */ | ||
251 | strsize=strlen(handle+rw); | ||
252 | tmp1=strstr(current_web_client->rbuf,"?"); | ||
253 | tmp3=strstr(current_web_client->rbuf," HTTP"); // End of GET header | ||
254 | if(tmp1!=NULL && tmp1<tmp3) { | ||
255 | tmp1+=1; | ||
256 | } else { | ||
257 | if(rw)return 0; | ||
258 | return defret; | ||
259 | } | ||
260 | |||
261 | // Working here | ||
262 | idf=0; | ||
263 | ret=defret; | ||
264 | seek=1; | ||
265 | tmp4=tmp1; | ||
266 | while(seek==1) { | ||
267 | tmp1=tmp4; | ||
268 | do { | ||
269 | tmp2=strstr(tmp1,handle+rw); | ||
270 | if(tmp2==NULL) { // must be nonnull | ||
271 | if(iddb!=NULL && iddb2!=NULL) { // if iddb2 is null then is just one value; | ||
272 | *iddb2=*iddb; | ||
273 | if(!rw)*iddb=*iddb-1; | ||
274 | }; | ||
275 | if(rw) { | ||
276 | if(ret==defret) return 0; | ||
277 | return (char *)*iddb2; | ||
278 | } | ||
279 | return ret; // if first null return defret (ret=defret); | ||
280 | |||
281 | }; | ||
282 | tmp1=tmp2+strsize; | ||
283 | } while ((tmp2[-1]!='?' && tmp2[-1]!='&') || tmp2[strsize]!='='); // Johannes E. Schindelin Fix | ||
284 | |||
285 | if(tmp3<tmp2) { | ||
286 | if(iddb!=NULL && iddb2!=NULL) { | ||
287 | *iddb2=*iddb; | ||
288 | if(!rw)*iddb=*iddb-1; | ||
289 | }; | ||
290 | if(rw) { | ||
291 | if(ret==defret) return 0; | ||
292 | return (char *)*iddb2; | ||
293 | } | ||
294 | |||
295 | return ret; | ||
296 | }; | ||
297 | |||
298 | tmp4=tmp1; | ||
299 | // if not null, so add an node; | ||
300 | |||
301 | // Working here ^ | ||
302 | ql->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Query)); | ||
303 | if(ql->next==NULL) { | ||
304 | LWSERR(LE_MEMORY); | ||
305 | if(handle[0]=='#') rw=1; | ||
306 | return defret; | ||
307 | }; | ||
308 | ql->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1); | ||
309 | if(ql->next->id==NULL) { | ||
310 | LWSERR(LE_MEMORY); | ||
311 | if(handle[0]=='#') rw=1; | ||
312 | return defret; | ||
313 | }; | ||
314 | memcpy(ql->next->id,handle+rw,strsize); | ||
315 | ql->next->id[strsize]=0; | ||
316 | if(idf==0) { | ||
317 | ql->next->index=0; | ||
318 | iddb=&ql->next->index; | ||
319 | iddb2=&ql->next->idf; // second holds information about number of fetchs; | ||
320 | |||
321 | }; | ||
322 | ql->next->data=defret; | ||
323 | ql->next->next=NULL; | ||
324 | |||
325 | |||
326 | tmp1=strstr(tmp2,"&"); // tmp1 goes to next '&' | ||
327 | tmp2+=strsize+1; // tmp2 goes to start of data | ||
328 | tmp3=strstr(tmp2," HTTP"); // tmp3 goes to the end of Get header | ||
329 | if(tmp1==NULL || ((unsigned int)tmp1>(unsigned int)tmp3)) { | ||
330 | size=tmp3-tmp2; // MUST HAVE (" HTTP") else, server don't let in | ||
331 | } else { | ||
332 | size=tmp1-tmp2; | ||
333 | }; | ||
334 | if(size<1) { | ||
335 | if(handle[0]=='#') rw=1; | ||
336 | return defret; | ||
337 | }; | ||
338 | |||
339 | |||
340 | ql->next->data=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
341 | if(ql->next->data==NULL) { | ||
342 | LWSERR(LE_MEMORY); | ||
343 | if(handle[0]=='#') rw=1; | ||
344 | return defret; | ||
345 | }; | ||
346 | j=0; | ||
347 | for(i=0;i<size;i++) { // Hex translation here | ||
348 | switch (ch=tmp2[j]) { | ||
349 | case '+': | ||
350 | ch=' '; | ||
351 | break; | ||
352 | case '%': | ||
353 | |||
354 | tmp1=__ILWS_malloc(3); | ||
355 | if(tmp1==NULL) { | ||
356 | LWSERR(LE_MEMORY); | ||
357 | if(rw) return 0; | ||
358 | return defret; | ||
359 | }; | ||
360 | strncpy(tmp1,&tmp2[j+1],2); | ||
361 | tmp1[2]=0; | ||
362 | ch=strtol(tmp1,NULL,16); | ||
363 | j+=2; | ||
364 | size-=2; | ||
365 | |||
366 | __ILWS_free(tmp1); | ||
367 | break; | ||
368 | }; | ||
369 | ql->next->data[i]=ch; | ||
370 | j++; | ||
371 | }; | ||
372 | ql->next->data[size]='\0'; | ||
373 | ret=ql->next->data; // to the last | ||
374 | ql=ql->next; | ||
375 | *iddb=*iddb+1; | ||
376 | idf++; | ||
377 | }; | ||
378 | return ret; | ||
379 | } | ||
380 | |||
381 | |||
382 | |||
383 | /*********************************************************************************************************/ | ||
384 | /* | ||
385 | * Function for Postdata | ||
386 | */ | ||
387 | char *__ILWS_Post(char *handle) { | ||
388 | char *tmp1,*tmp2,*tmp3,*ret; | ||
389 | struct _Post *pl=ClientInfo->PostList; | ||
390 | char *defret=""; | ||
391 | int *iddb=NULL,*iddb2=NULL; | ||
392 | int idf; | ||
393 | int seek=1; | ||
394 | size_t strsize; | ||
395 | size_t size; | ||
396 | int j=0,ch; | ||
397 | unsigned int i; | ||
398 | int rw=0; //return what; | ||
399 | |||
400 | tmp1=strstr(current_web_client->rbuf,"Content-type: multipart/form-data"); // multipart this post doesn't work | ||
401 | if(tmp1!=NULL) { | ||
402 | return ClientInfo->MultiPart(handle).data; | ||
403 | }; | ||
404 | if(handle==NULL) { | ||
405 | return ClientInfo->PostData; | ||
406 | }; | ||
407 | if(handle[0]=='#')rw=1; | ||
408 | /* Allocate the list */ | ||
409 | if(ClientInfo->PostList==NULL) { | ||
410 | if(!(ClientInfo->PostList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Post)))) { | ||
411 | LWSERR(LE_MEMORY); | ||
412 | if(rw) return 0; | ||
413 | return defret; | ||
414 | }; | ||
415 | ClientInfo->PostList->next=NULL; | ||
416 | ClientInfo->PostList->data=NULL; | ||
417 | ClientInfo->PostList->id=NULL; | ||
418 | pl=ClientInfo->PostList; | ||
419 | }; | ||
420 | |||
421 | // First search if exists | ||
422 | idf=1; | ||
423 | iddb=&idf; | ||
424 | seek=1; | ||
425 | while(pl->next!=NULL) { | ||
426 | if(pl->next->id!=NULL) { | ||
427 | if(!strcmp(pl->next->id,handle+rw) && iddb>=0) { | ||
428 | if(seek==1) { | ||
429 | iddb=&pl->next->index; | ||
430 | iddb2=&pl->next->idf; | ||
431 | if(rw) return (char *)(*iddb2); | ||
432 | if(pl->next->idf==1) { | ||
433 | return pl->next->data; | ||
434 | }; | ||
435 | j=*iddb; | ||
436 | seek++; | ||
437 | }; | ||
438 | *iddb=*iddb-1; | ||
439 | |||
440 | if(*iddb<=0) { | ||
441 | *iddb=j-1; | ||
442 | if(j<=1) { | ||
443 | *iddb=*iddb2; | ||
444 | |||
445 | //return defret; | ||
446 | }; | ||
447 | return pl->next->data; | ||
448 | }; | ||
449 | }; | ||
450 | }; | ||
451 | pl=pl->next; | ||
452 | }; | ||
453 | |||
454 | |||
455 | |||
456 | |||
457 | |||
458 | /* Doesn't exists */ | ||
459 | strsize=strlen(handle+rw); | ||
460 | tmp1=strstr(current_web_client->rbuf,"\r\n\r\n"); | ||
461 | if(tmp1!=NULL) | ||
462 | tmp1+=4; | ||
463 | else { | ||
464 | if(rw) return 0; | ||
465 | return defret; | ||
466 | }; | ||
467 | idf=0; | ||
468 | ret=defret; | ||
469 | seek=1; | ||
470 | tmp3=tmp1; | ||
471 | while(seek==1) { | ||
472 | tmp1=tmp3; | ||
473 | do { | ||
474 | tmp2=strstr(tmp1,handle+rw); | ||
475 | if(tmp2==NULL) { // mustn't be null | ||
476 | if(iddb!=NULL && iddb2!=NULL) { // if iddb2 is null then is just one value; | ||
477 | *iddb2=*iddb; | ||
478 | if(!rw)*iddb=*iddb-1; | ||
479 | }; | ||
480 | if(rw) { | ||
481 | if(ret==defret) return 0; | ||
482 | return (char *)*iddb2; | ||
483 | } | ||
484 | return ret; // if first null return defret (ret=defret); | ||
485 | |||
486 | }; | ||
487 | tmp1=tmp2+strsize; | ||
488 | } while ((tmp2[-1]!='\n' && tmp2[-1]!='&') || tmp2[strsize]!='='); // Johannes E. Schindelin Fix | ||
489 | tmp3=tmp1; | ||
490 | |||
491 | |||
492 | pl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Post)); | ||
493 | if(pl->next==NULL) { | ||
494 | LWSERR(LE_MEMORY); | ||
495 | if(rw) return 0; | ||
496 | return defret; | ||
497 | }; | ||
498 | pl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1); | ||
499 | if(pl->next->id==NULL) { | ||
500 | LWSERR(LE_MEMORY); | ||
501 | if(rw) return 0; | ||
502 | return defret; | ||
503 | }; | ||
504 | memcpy(pl->next->id,handle+rw,strsize); | ||
505 | pl->next->id[strsize]=0; | ||
506 | if(idf==0) { | ||
507 | pl->next->index=0; | ||
508 | iddb=&pl->next->index; | ||
509 | iddb2=&pl->next->idf; | ||
510 | }; | ||
511 | |||
512 | pl->next->data=defret; | ||
513 | pl->next->next=NULL; | ||
514 | |||
515 | tmp1=strstr(tmp2,"&"); // goes to the next & (end of data) | ||
516 | tmp2+=strsize+1; // tmp2 goes to start of data | ||
517 | if(tmp1==NULL) { | ||
518 | size=strlen(tmp2); | ||
519 | } else { | ||
520 | size=tmp1-tmp2; | ||
521 | }; | ||
522 | if(size==0) { | ||
523 | if(rw) return 0; | ||
524 | return defret; | ||
525 | }; | ||
526 | |||
527 | pl->next->data=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
528 | if(pl->next->data==NULL) { | ||
529 | LWSERR(LE_MEMORY); | ||
530 | return defret; | ||
531 | }; | ||
532 | j=0; | ||
533 | for(i=0;i<size;i++) { // hex translation here | ||
534 | switch (ch=tmp2[j]) { | ||
535 | case '+': | ||
536 | ch=' '; | ||
537 | break; | ||
538 | case '%': | ||
539 | |||
540 | tmp1=__ILWS_malloc(3); | ||
541 | if(tmp1==NULL) { | ||
542 | LWSERR(LE_MEMORY); | ||
543 | if(rw) return 0; | ||
544 | return defret; | ||
545 | }; | ||
546 | strncpy(tmp1,&tmp2[j+1],2); | ||
547 | tmp1[2]=0; | ||
548 | |||
549 | ch=strtol(tmp1,NULL,16); | ||
550 | j+=2; | ||
551 | size-=2; | ||
552 | |||
553 | __ILWS_free(tmp1); | ||
554 | break; | ||
555 | }; | ||
556 | pl->next->data[i]=ch; | ||
557 | j++; | ||
558 | }; | ||
559 | pl->next->data[size]='\0'; | ||
560 | ret=pl->next->data; // to the last | ||
561 | *iddb=*iddb+1; | ||
562 | idf++; | ||
563 | pl=pl->next; | ||
564 | //pl->next->data=ret; | ||
565 | }; | ||
566 | return ret; | ||
567 | } | ||
568 | |||
569 | |||
570 | |||
571 | /*********************************************************************************************************/ | ||
572 | /* | ||
573 | * Function for MultiPart formdata | ||
574 | */ | ||
575 | struct _MultiPart __ILWS_MultiPart(char *handle) { | ||
576 | char *tmp1,*tmp2,*tmp3; | ||
577 | int i; | ||
578 | char *name; | ||
579 | size_t namesize; | ||
580 | struct _MultiPart *ml=ClientInfo->MultiPartList; | ||
581 | struct _MultiPart defret={"","",0,""}; | ||
582 | size_t strsize; | ||
583 | char *boundary; size_t boundarysize; | ||
584 | // IE C43o6Fn6Et74e65n6Et74-2DT54y79p70e65:3A 20m6Du75l6Ct74i69p70a61r72t74/2Ff66o6Fr72m6D-2Dd64a61t74a61 | ||
585 | // NS C43o6Fn6Et74e65n6Et74-2Dt74y79p70e65:3A 20m6Du75l6Ct74i69p70a61r72t74/2Ff66o6Fr72m6D-2Dd64a61t74a61 | ||
586 | tmp1=__ILWS_stristr(current_web_client->rbuf,"Content-type: multipart/form-data"); | ||
587 | if(tmp1==NULL) return defret; | ||
588 | if(ClientInfo->MultiPartList==NULL) { | ||
589 | ClientInfo->MultiPartList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _MultiPart)); | ||
590 | if(ClientInfo->MultiPartList==NULL) { | ||
591 | LWSERR(LE_MEMORY); | ||
592 | return defret; | ||
593 | }; | ||
594 | ClientInfo->MultiPartList->next=NULL; | ||
595 | ClientInfo->MultiPartList->id=NULL; | ||
596 | ClientInfo->MultiPartList->data=NULL; | ||
597 | ClientInfo->MultiPartList->filename=NULL; | ||
598 | ClientInfo->MultiPartList->size=0; | ||
599 | ml=ClientInfo->MultiPartList; | ||
600 | }; | ||
601 | // Check if handle exists | ||
602 | while(ml->next!=NULL) { | ||
603 | if(ml->next->id!=NULL) { | ||
604 | if(!strcmp(ml->next->id,handle)) { | ||
605 | |||
606 | return *ml->next; | ||
607 | }; | ||
608 | }; | ||
609 | ml=ml->next; | ||
610 | }; | ||
611 | |||
612 | |||
613 | strsize=strlen(handle); | ||
614 | ml->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _MultiPart)); | ||
615 | if(ml->next==NULL) { | ||
616 | LWSERR(LE_MEMORY); | ||
617 | return defret; | ||
618 | }; | ||
619 | ml->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1); | ||
620 | if(ml->next->id==NULL) { | ||
621 | LWSERR(LE_MEMORY); | ||
622 | return defret; | ||
623 | }; | ||
624 | memcpy(ml->next->id,handle,strsize); | ||
625 | ml->next->id[strsize]=0; | ||
626 | ml->next->data=""; | ||
627 | ml->next->filename=""; | ||
628 | ml->next->size=0; | ||
629 | ml->next->next=NULL; | ||
630 | |||
631 | tmp1=strstr(tmp1,"boundary="); | ||
632 | if(tmp1==NULL) return defret; | ||
633 | tmp1+=9; | ||
634 | tmp2=strstr(tmp1,"\r\n"); | ||
635 | if(tmp2<tmp1 || tmp2==NULL) return defret; | ||
636 | /* boundary */ | ||
637 | boundarysize=tmp2-tmp1; | ||
638 | boundary=__ILWS_add_buffer(ClientInfo->mem,boundarysize+3); | ||
639 | if(boundary==NULL) { | ||
640 | LWSERR(LE_MEMORY); | ||
641 | return defret; | ||
642 | }; | ||
643 | memcpy(boundary,tmp1,boundarysize); | ||
644 | boundary[boundarysize]=0; | ||
645 | |||
646 | |||
647 | /* handle */ | ||
648 | namesize=boundarysize+41+strlen(handle); | ||
649 | name=__ILWS_add_buffer(ClientInfo->mem,namesize+1); | ||
650 | if(name==NULL) { | ||
651 | LWSERR(LE_MEMORY); | ||
652 | return defret; | ||
653 | }; | ||
654 | snprintf(name,namesize,"%s\r\nContent-Disposition: form-data; name=",boundary); | ||
655 | namesize=strlen(name); | ||
656 | |||
657 | tmp1=strstr(tmp1,"\r\n\r\n"); // go to data | ||
658 | if(tmp1==NULL) return defret; | ||
659 | |||
660 | do { | ||
661 | i=memcmp(tmp1,name,namesize); | ||
662 | if(i==0) { | ||
663 | tmp1+=namesize; | ||
664 | if(tmp1[0]=='\"')tmp1+=1; | ||
665 | if(strncmp(tmp1,handle,strlen(handle))){ | ||
666 | i=1; | ||
667 | }else { | ||
668 | if((tmp1[strsize]!=' ') && (tmp1[strsize]!='\"') && (tmp1[strsize]!='\r') && (tmp1[strsize]!=';') ) i=1; | ||
669 | }; | ||
670 | |||
671 | }else { | ||
672 | tmp1+=1; | ||
673 | }; | ||
674 | } while(i!=0 && (tmp1+namesize<current_web_client->rbuf+current_web_client->rbufsize)); // Search init of data | ||
675 | if(i!=0) return defret; | ||
676 | //tmp1+=namesize; | ||
677 | tmp2=strstr(tmp1,"filename="); // get filename | ||
678 | if(tmp2!=NULL) { | ||
679 | tmp2+=9; | ||
680 | if(tmp2[0]=='\"')tmp2+=1; | ||
681 | tmp3=strstr(tmp2,"\r\n"); | ||
682 | ml->next->filename=__ILWS_add_buffer(ClientInfo->mem,(tmp3-tmp2)+1); | ||
683 | if(ml->next->filename==NULL) { | ||
684 | LWSERR(LE_MEMORY); | ||
685 | return defret; | ||
686 | }; | ||
687 | memcpy(ml->next->filename,tmp2,tmp3-tmp2); | ||
688 | ml->next->filename[tmp3-tmp2]='\0'; | ||
689 | if(ml->next->filename[tmp3-tmp2-1]=='\"') | ||
690 | ml->next->filename[tmp3-tmp2-1]='\0'; | ||
691 | |||
692 | }; | ||
693 | tmp2=strstr(tmp1,"\r\n\r\n"); // data init | ||
694 | if(tmp2==NULL)return defret; | ||
695 | tmp2+=4; | ||
696 | tmp3=tmp2; | ||
697 | do { | ||
698 | |||
699 | i=memcmp(tmp3,boundary,boundarysize); | ||
700 | if(i!=0)tmp3+=1; | ||
701 | } while(i!=0 && (tmp3+boundarysize<current_web_client->rbuf+current_web_client->rbufsize)); // End of data | ||
702 | if(i!=0) return defret; | ||
703 | tmp3-=4; // back "\r\n\r\n" | ||
704 | |||
705 | // copy data to node | ||
706 | if(!(ml->next->data=__ILWS_add_buffer(ClientInfo->mem,(tmp3-tmp2)+1))) { | ||
707 | LWSERR(LE_MEMORY); | ||
708 | return defret; | ||
709 | }; | ||
710 | memcpy(ml->next->data,tmp2,tmp3-tmp2); | ||
711 | ml->next->data[tmp3-tmp2]='\0'; | ||
712 | ml->next->size=tmp3-tmp2; | ||
713 | |||
714 | |||
715 | |||
716 | |||
717 | return *ml->next; | ||
718 | |||
719 | }; | ||
720 | |||
721 | /*********************************************************************************************************/ | ||
722 | /* | ||
723 | * Function for CookieData | ||
724 | */ | ||
725 | char *__ILWS_Cookie(char *handle) { | ||
726 | char *defret=""; | ||
727 | char *tmp1,*tmp2,*ret; | ||
728 | int size; | ||
729 | int strsize; | ||
730 | struct _Cookie *cl=ClientInfo->CookieList; | ||
731 | |||
732 | |||
733 | tmp1=strstr(current_web_client->rbuf,"\nCookie: "); // start of cookie string | ||
734 | if(tmp1==NULL) { // no cookies | ||
735 | return defret; | ||
736 | }; | ||
737 | tmp1+=8; | ||
738 | if(handle==NULL) { | ||
739 | return ClientInfo->CookieString; | ||
740 | }; | ||
741 | |||
742 | if(ClientInfo->CookieList==NULL) { | ||
743 | |||
744 | ClientInfo->CookieList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Cookie)); | ||
745 | if(ClientInfo->CookieList==NULL) { | ||
746 | LWSERR(LE_MEMORY); | ||
747 | return defret; | ||
748 | }; | ||
749 | ClientInfo->CookieList->next=NULL; | ||
750 | ClientInfo->CookieList->data=NULL; | ||
751 | ClientInfo->CookieList->id=NULL; | ||
752 | cl=ClientInfo->CookieList; | ||
753 | } | ||
754 | // First search if exists | ||
755 | while(cl->next!=NULL) { | ||
756 | if(cl->next->id!=NULL) { | ||
757 | if(!strcmp(cl->next->id,handle)) { | ||
758 | |||
759 | return cl->next->data; | ||
760 | }; | ||
761 | }; | ||
762 | cl=cl->next; | ||
763 | }; | ||
764 | |||
765 | strsize=strlen(handle); | ||
766 | if(!(cl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Cookie)))) { | ||
767 | LWSERR(LE_MEMORY); | ||
768 | return defret; | ||
769 | }; | ||
770 | if(!(cl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1))) { | ||
771 | LWSERR(LE_MEMORY); | ||
772 | return defret; | ||
773 | }; | ||
774 | memcpy(cl->next->id,handle,strsize); | ||
775 | cl->next->id[strsize]=0; | ||
776 | cl->next->data=defret; | ||
777 | cl->next->next=NULL; | ||
778 | do { | ||
779 | tmp2=strstr(tmp1,handle); | ||
780 | if(tmp2==NULL) { | ||
781 | return defret; | ||
782 | }else if(tmp2[strsize]==';' && tmp2[-1]==' ') { | ||
783 | cl->next->data=__ILWS_add_buffer(ClientInfo->mem,6); | ||
784 | snprintf(cl->next->data,5,"True"); | ||
785 | return cl->next->data; | ||
786 | }; | ||
787 | tmp1=tmp2+strsize; | ||
788 | }while(tmp2[-1]!=' ' || tmp2[strsize]!='='); | ||
789 | |||
790 | tmp1=strstr(tmp2,";"); // end of data | ||
791 | tmp2+=strsize+1; // start of data | ||
792 | if(tmp1==NULL) { | ||
793 | size=strstr(tmp2,"\r")-tmp2; | ||
794 | |||
795 | } else { | ||
796 | size=tmp1-tmp2; | ||
797 | }; | ||
798 | if(size<1) { | ||
799 | return defret; | ||
800 | }; | ||
801 | |||
802 | ret=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
803 | if(ret==NULL) { | ||
804 | LWSERR(LE_MEMORY); | ||
805 | return defret; | ||
806 | }; | ||
807 | |||
808 | memcpy(ret,tmp2,size); | ||
809 | ret[size]='\0'; | ||
810 | cl->next->data=ret; | ||
811 | return cl->next->data; | ||
812 | }; | ||
813 | |||
814 | |||
815 | |||
816 | /*********************************************************************************************************/ | ||
817 | /* | ||
818 | * get whole query string | ||
819 | */ | ||
820 | char *__ILWS_clientinfo_getquerystring() { | ||
821 | char *tmp1,*tmp2,*ret; | ||
822 | char *defret=""; | ||
823 | size_t size; | ||
824 | tmp1=strstr(current_web_client->rbuf,"?"); | ||
825 | tmp2=strstr(current_web_client->rbuf,"HTTP"); | ||
826 | if(tmp1!=NULL && tmp1<tmp2) | ||
827 | tmp1+=1; | ||
828 | else | ||
829 | return defret; | ||
830 | size=(tmp2-tmp1)-1; | ||
831 | ret=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
832 | if(ret==NULL) { | ||
833 | LWSERR(LE_MEMORY); | ||
834 | return defret; | ||
835 | }; | ||
836 | memcpy(ret,tmp1,size); | ||
837 | ret[size]=0; | ||
838 | return ret; | ||
839 | }; | ||
840 | |||
841 | |||
842 | /*********************************************************************************************************/ | ||
843 | /* | ||
844 | * get whole post data | ||
845 | */ | ||
846 | char *__ILWS_clientinfo_getpostdata() { | ||
847 | char *tmp1,*ret; | ||
848 | char *defret=""; | ||
849 | size_t size; | ||
850 | tmp1=strstr(current_web_client->rbuf,"\r\n\r\n"); | ||
851 | if(tmp1!=NULL && (tmp1+4)<(char*)(current_web_client->rbuf+current_web_client->rbufsize)) | ||
852 | tmp1+=4; | ||
853 | else | ||
854 | return defret; | ||
855 | size=(current_web_client->rbuf+current_web_client->rbufsize)-tmp1; | ||
856 | ret=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
857 | if(ret==NULL) { | ||
858 | LWSERR(LE_MEMORY); | ||
859 | return defret; | ||
860 | }; | ||
861 | memcpy(ret,tmp1,size); | ||
862 | ret[size]='\0'; | ||
863 | return ret; | ||
864 | } | ||
865 | |||
866 | |||
867 | /*********************************************************************************************************/ | ||
868 | /* | ||
869 | * Get authorization username | ||
870 | */ | ||
871 | char *__ILWS_clientinfo_getauthuser() { | ||
872 | char *tmp1,*tmp2,*ret, *out=NULL; | ||
873 | char *defret=""; | ||
874 | size_t size; | ||
875 | |||
876 | tmp1=strstr(current_web_client->rbuf,"Authorization: Basic"); | ||
877 | if(tmp1==NULL) { | ||
878 | |||
879 | return defret; | ||
880 | }; | ||
881 | |||
882 | tmp1+=21; | ||
883 | tmp2=strstr(tmp1,"\r\n"); | ||
884 | if(tmp2==NULL) return defret; | ||
885 | size=(int)(tmp2-tmp1); | ||
886 | |||
887 | ret=__ILWS_malloc(size+1); | ||
888 | if(ret==NULL) { | ||
889 | LWSERR(LE_MEMORY); | ||
890 | return defret; | ||
891 | }; | ||
892 | memcpy(ret,tmp1,size); | ||
893 | ret[size]=0; | ||
894 | |||
895 | out=__ILWS_malloc(size+1); | ||
896 | if(out==NULL) { | ||
897 | LWSERR(LE_MEMORY); | ||
898 | __ILWS_free(ret); | ||
899 | return defret; | ||
900 | }; | ||
901 | |||
902 | size=__ILWS_base64decode(out,ret); | ||
903 | out[size]='\0'; | ||
904 | |||
905 | |||
906 | __ILWS_free(ret); | ||
907 | tmp2=strstr(out,":"); | ||
908 | if(tmp2==NULL) return defret; | ||
909 | |||
910 | ret=__ILWS_add_buffer(ClientInfo->mem,(tmp2-out)+1); | ||
911 | if(ret==NULL) { | ||
912 | LWSERR(LE_MEMORY); | ||
913 | __ILWS_free(out); | ||
914 | return defret; | ||
915 | }; | ||
916 | memcpy(ret,out,tmp2-out); | ||
917 | ret[tmp2-out]=0; | ||
918 | |||
919 | __ILWS_free(out); | ||
920 | return ret; | ||
921 | } | ||
922 | |||
923 | |||
924 | /*********************************************************************************************************/ | ||
925 | /* | ||
926 | * get authorization password | ||
927 | */ | ||
928 | char *__ILWS_clientinfo_getauthpass() { | ||
929 | char *tmp1,*tmp2,*ret, *out=NULL; | ||
930 | char *defret=""; | ||
931 | size_t size; | ||
932 | |||
933 | tmp1=strstr(current_web_client->rbuf,"Authorization: Basic"); | ||
934 | if(tmp1==NULL) { | ||
935 | |||
936 | return defret; | ||
937 | }; | ||
938 | |||
939 | tmp1+=21; | ||
940 | tmp2=strstr(tmp1,"\r\n"); | ||
941 | if(tmp2==NULL) return defret; | ||
942 | size=(int)(tmp2-tmp1); | ||
943 | |||
944 | ret=__ILWS_malloc(size+1); | ||
945 | if(ret==NULL) { | ||
946 | LWSERR(LE_MEMORY); | ||
947 | return defret; | ||
948 | }; | ||
949 | memcpy(ret,tmp1,size); | ||
950 | ret[size]=0; | ||
951 | |||
952 | out=__ILWS_malloc(size+1); | ||
953 | if(out==NULL) { | ||
954 | LWSERR(LE_MEMORY); | ||
955 | __ILWS_free(ret); | ||
956 | return defret; | ||
957 | }; | ||
958 | |||
959 | size=__ILWS_base64decode(out,ret); | ||
960 | out[size]='\0'; | ||
961 | |||
962 | |||
963 | __ILWS_free(ret); | ||
964 | tmp1=strstr(out,":")+1; | ||
965 | tmp2=out+strlen(out); | ||
966 | |||
967 | ret=__ILWS_add_buffer(ClientInfo->mem,(tmp2-tmp1)+1); | ||
968 | if(ret==NULL) { | ||
969 | LWSERR(LE_MEMORY); | ||
970 | __ILWS_free(out); | ||
971 | return defret; | ||
972 | }; | ||
973 | memcpy(ret,tmp1,tmp2-tmp1); | ||
974 | ret[tmp2-tmp1]=0; | ||
975 | |||
976 | __ILWS_free(out); | ||
977 | return ret; | ||
978 | } | ||
979 | |||
980 | |||
981 | /*********************************************************************************************************/ | ||
982 | /* | ||
983 | * get method (GET POST HEAD etc) | ||
984 | */ | ||
985 | char *__ILWS_clientinfo_getmethod() { | ||
986 | char *tmp1,*ret; | ||
987 | char *defret=""; | ||
988 | size_t size; | ||
989 | tmp1=strstr(current_web_client->rbuf," "); // first space | ||
990 | if(tmp1==NULL) { | ||
991 | return defret; | ||
992 | }; | ||
993 | size=tmp1-current_web_client->rbuf; | ||
994 | ret=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
995 | if(ret==NULL) { | ||
996 | LWSERR(LE_MEMORY); | ||
997 | return defret; | ||
998 | }; | ||
999 | memcpy(ret,current_web_client->rbuf,size); | ||
1000 | ret[size]=0; | ||
1001 | return ret; | ||
1002 | } | ||
1003 | |||
1004 | |||
1005 | /*********************************************************************************************************/ | ||
1006 | /* | ||
1007 | * get request name (GET /taltal HTTP/1.0) returning /taltal | ||
1008 | */ | ||
1009 | char *__ILWS_clientinfo_getreqname() { | ||
1010 | char *ret; | ||
1011 | char *tmp1=strstr(current_web_client->rbuf,"/"); // Must have / | ||
1012 | char *tmp2=strstr(tmp1,"?"); | ||
1013 | char *tmp3=strstr(tmp1," HTTP"); | ||
1014 | char *defret=""; | ||
1015 | size_t i,j; | ||
1016 | int ch; | ||
1017 | size_t size=0; | ||
1018 | if(tmp1==NULL || tmp3==NULL) return defret; | ||
1019 | if(tmp2==NULL || tmp2>tmp3) { | ||
1020 | tmp2=tmp3; | ||
1021 | }; | ||
1022 | //tmp1+=1; | ||
1023 | size=tmp2-tmp1; | ||
1024 | if(size<1) | ||
1025 | return defret; | ||
1026 | ret=__ILWS_add_buffer(ClientInfo->mem,size+1); | ||
1027 | if(ret==NULL) { | ||
1028 | LWSERR(LE_MEMORY); | ||
1029 | return defret; | ||
1030 | }; | ||
1031 | j=0; | ||
1032 | for(i=0;i<size;i++) { // hex translation here | ||
1033 | switch (ch=tmp1[j]) { | ||
1034 | case '+': | ||
1035 | ch=' '; | ||
1036 | break; | ||
1037 | case '%': | ||
1038 | |||
1039 | tmp2=__ILWS_malloc(3); | ||
1040 | if(tmp2==NULL) { | ||
1041 | LWSERR(LE_MEMORY); | ||
1042 | return defret; | ||
1043 | }; | ||
1044 | strncpy(tmp2,&tmp1[j+1],2); | ||
1045 | tmp2[2]=0; | ||
1046 | |||
1047 | ch=strtol(tmp2,NULL,16); | ||
1048 | j+=2; | ||
1049 | size-=2; | ||
1050 | __ILWS_free(tmp2); | ||
1051 | break; | ||
1052 | }; | ||
1053 | ret[i]=ch; | ||
1054 | j++; | ||
1055 | }; | ||
1056 | //pl->next->data[size]='\0'; | ||
1057 | //memcpy(ret,tmp1,size); | ||
1058 | ret[size]=0; | ||
1059 | return ret; | ||
1060 | } | ||
1061 | /*********************************************************************************************************/ | ||
1062 | /* | ||
1063 | * Get config entry (new on 0.5.0) | ||
1064 | */ | ||
1065 | char *__ILWS_Conf(const char *topic,const char *key) { | ||
1066 | struct web_server *server=current_web_server; | ||
1067 | FILE *tmpf; | ||
1068 | struct stat statf; // tested only on WIN | ||
1069 | char *defret=""; | ||
1070 | char *dataconf; | ||
1071 | char *tmp1,*tmp2,*tmp3; | ||
1072 | long tmpsize=0; | ||
1073 | int sizec; | ||
1074 | // Config revive tested only on WIN | ||
1075 | if(server->conffile!=NULL) { | ||
1076 | stat(server->conffile,&statf); | ||
1077 | if(statf.st_mtime>server->conffiletime) { | ||
1078 | tmpf=fopen(server->conffile,"r"); | ||
1079 | if(tmpf!=NULL) { | ||
1080 | free(server->dataconf); | ||
1081 | fseek(tmpf,SEEK_SET,SEEK_END); | ||
1082 | sizec=ftell(tmpf); | ||
1083 | fseek(tmpf,0,SEEK_SET); | ||
1084 | server->dataconf=malloc(sizec+1); | ||
1085 | fread(server->dataconf,sizec,1,tmpf); | ||
1086 | server->dataconf[sizec-9]=0; // 9 is temporary | ||
1087 | server->conffiletime=statf.st_mtime; | ||
1088 | fclose(tmpf); | ||
1089 | }; | ||
1090 | }; | ||
1091 | }; | ||
1092 | |||
1093 | dataconf=__ILWS_stristr(server->dataconf,topic); | ||
1094 | if(dataconf==NULL) { | ||
1095 | return defret; | ||
1096 | }; | ||
1097 | dataconf+=strlen(topic); | ||
1098 | |||
1099 | do { | ||
1100 | tmp1=__ILWS_stristr(dataconf,key); | ||
1101 | dataconf+=1; | ||
1102 | if(dataconf[0]==0) { | ||
1103 | return defret; | ||
1104 | }; | ||
1105 | if(dataconf[0]=='[' && dataconf[-1]=='\n') { | ||
1106 | return defret; | ||
1107 | }; | ||
1108 | }while(!(tmp1!=NULL && tmp1[-1]=='\n' && tmp1[strlen(key)]=='=')); | ||
1109 | |||
1110 | |||
1111 | tmp1+=strlen(key)+1; | ||
1112 | tmp2=__ILWS_stristr(tmp1,"\n"); | ||
1113 | if(tmp2==NULL) { | ||
1114 | tmp2=tmp1+strlen(tmp1); | ||
1115 | }; | ||
1116 | tmpsize=tmp2-tmp1; | ||
1117 | tmp3=__ILWS_add_buffer(ClientInfo->mem,tmpsize+1); | ||
1118 | memcpy(tmp3,tmp1,tmpsize); | ||
1119 | tmp3[tmpsize]=0; | ||
1120 | return tmp3; | ||
1121 | |||
1122 | |||
1123 | |||
1124 | }; | ||