aboutsummaryrefslogtreecommitdiff
path: root/src/clientinfo.c
blob: 0aa5e3b5cb972d93e3d8347db5accf3432f15e82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved. 
 *
 * See the LICENSE file
 *
 * The origin of this software must not be misrepresented, either by
 * explicit claim or by omission.  Since few users ever read sources,
 * credits must appear in the documentation.
 *
 * date: Wed Oct  9 19:56:22 GMT 2002
 *
 * -- parse http header into "ClientInfo"
 *
 */

#include "clientinfo.h"

/*********************************************************************************************************/
/*
 * Initialize ClientInfo structure
 */
void __ILWS_init_clientinfo(struct ClientInfo *ClientInfo) { 
	char *t;
	struct outstream *tstream=current_web_client->outstream;
	
	ClientInfo=__ILWS_malloc(sizeof(struct ClientInfo));
	if(ClientInfo==NULL) {
		return;
	};
	
	while(tstream->next!=NULL) {
		tstream=tstream->next;
	};
	
	if(tstream->fstream!=NULL) ClientInfo->outfd=fileno(tstream->fstream); //take it off?
		
	ClientInfo->mem=__ILWS_init_buffer_list(); // First thing, other fuctions use this to allocate
	
        
	ClientInfo->request=__ILWS_clientinfo_getreqname();
	
	ClientInfo->inetname=NULL;
	t=inet_ntoa(current_web_client->sa.sin_addr);
	if((ClientInfo->inetname=__ILWS_add_buffer(ClientInfo->mem,strlen(t)+1))) {
		memcpy(ClientInfo->inetname,t,strlen(t));
		ClientInfo->inetname[strlen(t)]='\0';
	};
	
	ClientInfo->method=__ILWS_clientinfo_getmethod();
	ClientInfo->user=__ILWS_clientinfo_getauthuser();
	ClientInfo->pass=__ILWS_clientinfo_getauthpass();
	
	
	
	/* Initialize List's */
	ClientInfo->HeaderList=NULL;
	ClientInfo->QueryList=NULL;
	ClientInfo->PostList=NULL;
	ClientInfo->MultiPartList=NULL;
	ClientInfo->CookieList=NULL;

	ClientInfo->Header=__ILWS_Header;
	ClientInfo->Query=__ILWS_Query;
	ClientInfo->QueryString=__ILWS_clientinfo_getquerystring();	
	ClientInfo->Post=__ILWS_Post;
	ClientInfo->PostData=__ILWS_clientinfo_getpostdata();	
	ClientInfo->MultiPart=__ILWS_MultiPart;
	ClientInfo->Cookie=__ILWS_Cookie;
	ClientInfo->Conf=__ILWS_Conf;
	ClientInfo->CookieString=__ILWS_Header("Cookie");
	
}                      

/*
 * Free ClientInfo structure
 */
void __ILWS_free_clientinfo(struct ClientInfo *ClientInfo) {
  if(ClientInfo==NULL) {	
    return;
  };
  __ILWS_delete_buffer_list(ClientInfo->mem); 
  
  __ILWS_free(ClientInfo);
  ClientInfo=NULL;
}


/*
 * Header function for ClientInfo->Header("x")
 */
char *__ILWS_Header(struct ClientInfo *ClientInfo, char *str) {
	char *tmp1,*tmp2,*tmp3,*ret;
	struct _Header *hl=ClientInfo->HeaderList;
	char *defret="";
	size_t size;
	size_t strsize;
	if(str==NULL) { // request is null return whole header
		return current_web_client->rbuf;
	};
	if(ClientInfo->HeaderList==NULL) {
		
		ClientInfo->HeaderList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Header));
		if(ClientInfo->HeaderList==NULL) {
			return defret;
		};
		ClientInfo->HeaderList->next=NULL;
		ClientInfo->HeaderList->data=NULL;
		ClientInfo->HeaderList->id=NULL;
		hl=ClientInfo->HeaderList;
	};
	// First search if exists
	
	while(hl->next!=NULL) {
		if(hl->next->id!=NULL) {
			if(!strcmp(hl->next->id,str)) {
				
				return hl->next->data;
			};
		};
		hl=hl->next;	
	};
	
	/* Doesn't exists	 */
	strsize=strlen(str);
	if(!(hl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Header)))) {
		return defret;
	};
	if(!(hl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1))) {
		return defret;
	};
	
	memcpy(hl->next->id,str,strsize);
	hl->next->id[strsize]=0;	
	hl->next->data=defret;
	hl->next->next=NULL;

	if(!(tmp3=__ILWS_malloc(strsize+3))) {
		return defret;
	};
	snprintf(tmp3,strsize+3,"%s: ",str);
	tmp1=__ILWS_stristr(current_web_client->rbuf,tmp3);
	__ILWS_free(tmp3);
	if(tmp1==NULL) {
		return defret;
	};
	
	tmp1+=strsize+2;
	if(!(tmp2=strstr(tmp1,"\r\n"))) { // Unexpected (security anyway)
		return defret;
	};
	if((size=(unsigned int)(tmp2-tmp1))<0) {
		return defret;
	};
	if(!(ret=__ILWS_add_buffer(ClientInfo->mem,size+1))) { //malloc & register
		return defret;
	};
	memcpy(ret,tmp1,size);
	ret[size]=0;
	hl->next->data=ret;
	return ret;
}                                

/*
 * Function for Querydata
 */
char *__ILWS_Query(struct ClientInfo *ClientInfo, char *handle) {
    char *tmp1,*tmp2,*tmp3,*tmp4,*ret;
	char *defret="";
	size_t strsize;
    size_t size;
	int j=0,ch;
	int seek=1;
	unsigned int i;
	unsigned int *iddb=NULL;
	unsigned int *iddb2=NULL;
	unsigned int idf=0;
	int rw=0; // 0 data 1 number of vars; (return what?)
	struct _Query *ql=ClientInfo->QueryList;
	
	
	if(handle==NULL) {
		return ClientInfo->QueryString;
	};
	if(handle[0]=='#') rw=1;
	// allocate first node from the list 
	if(ClientInfo->QueryList==NULL) {                                                              
		ClientInfo->QueryList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Query));                   
		if(ClientInfo->QueryList==NULL) {                                                          
			if(rw) return 0;
			return defret;
		};
		ClientInfo->QueryList->next=NULL;
		ClientInfo->QueryList->data=NULL;
		ClientInfo->QueryList->id=NULL;
		ql=ClientInfo->QueryList;
	};
	// done allocating


	// First search if exists and fetch values;
	
	idf=1;
	iddb=&idf;
	seek=1;
	
	while(ql->next!=NULL) {
		if(ql->next->id!=NULL) {
			if(!strcmp(ql->next->id,handle+rw) && *iddb >= 0) {
				if(seek==1) {
					iddb=&ql->next->index; // atribute iddb to first node
					iddb2=&ql->next->idf; // atribute iddb2 to counting
					if(rw) return (char *)*iddb2;
					if(ql->next->idf==1) {
						return ql->next->data;
					};
					j=*iddb;
					seek++;			
				};
				*iddb=*iddb-1;
				
				if(*iddb<=0) {
					*iddb=j-1;
					if(j<=1) {
						*iddb=*iddb2; // go to start if any
						//return defret; // to be null
					};
					return ql->next->data; // Return existent
				};
				
			};
		};
		ql=ql->next;	
	};
	
	
	
	/* Doesn't exists	 */
	strsize=strlen(handle+rw);
	tmp1=strstr(current_web_client->rbuf,"?"); 
	tmp3=strstr(current_web_client->rbuf," HTTP"); // End of GET header
	if(tmp1!=NULL && tmp1<tmp3) {
		tmp1+=1;
	} else {
		if(rw)return 0;
		return defret;
	}
	
	// Working here
	idf=0;
	ret=defret;
	seek=1;
	tmp4=tmp1;
	while(seek==1) {
		tmp1=tmp4;
		do {
			tmp2=strstr(tmp1,handle+rw);
			if(tmp2==NULL) { // must be nonnull
				if(iddb!=NULL && iddb2!=NULL) { // if iddb2 is null then is just one value;
					*iddb2=*iddb;
					if(!rw)*iddb=*iddb-1;
				};
				if(rw) {
					if(ret==defret) return 0;
					return (char *)*iddb2;
				} 
				return ret; // if first null return defret (ret=defret);
				
			};
			tmp1=tmp2+strsize;
		} while ((tmp2[-1]!='?' && tmp2[-1]!='&') || tmp2[strsize]!='='); // Johannes E. Schindelin Fix

		if(tmp3<tmp2) { 
			if(iddb!=NULL && iddb2!=NULL) {
				*iddb2=*iddb;
				if(!rw)*iddb=*iddb-1;
			};
			if(rw) {
				if(ret==defret) return 0;
				return (char *)*iddb2;
			}
			
			return ret;
		};
	
		tmp4=tmp1;
			// if not null, so add an node;
	
		// Working here ^
		ql->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Query));
		if(ql->next==NULL) {
			if(handle[0]=='#') rw=1;
			return defret;
		};
		ql->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1);
		if(ql->next->id==NULL) {
			if(handle[0]=='#') rw=1;
			return defret;
		};
		memcpy(ql->next->id,handle+rw,strsize);
		ql->next->id[strsize]=0;	
		if(idf==0) {
			ql->next->index=0;
			iddb=&ql->next->index;
			iddb2=&ql->next->idf; // second holds information about number of fetchs;
			
		};
		ql->next->data=defret;
		ql->next->next=NULL;

		
		tmp1=strstr(tmp2,"&"); // tmp1 goes to next '&'
		tmp2+=strsize+1;           // tmp2 goes to start of data
		tmp3=strstr(tmp2," HTTP"); // tmp3 goes to the end of Get header
		if(tmp1==NULL || ((unsigned int)tmp1>(unsigned int)tmp3)) {
			size=tmp3-tmp2; // MUST HAVE (" HTTP") else, server don't let in
		} else {
			size=tmp1-tmp2;
		};
		if(size<1) {
			if(handle[0]=='#') rw=1;
			return defret;
		};
		
		
		ql->next->data=__ILWS_add_buffer(ClientInfo->mem,size+1);
		if(ql->next->data==NULL) {
			if(handle[0]=='#') rw=1;
			return defret;
		};
		j=0;
		for(i=0;i<size;i++) { // Hex translation here
			switch (ch=tmp2[j]) {
			case '+':
				ch=' ';
				break;
			case '%':
				
				tmp1=__ILWS_malloc(3);
				if(tmp1==NULL) {
					if(rw) return 0;
					return defret;
				};
				strncpy(tmp1,&tmp2[j+1],2);
				tmp1[2]=0;
				ch=strtol(tmp1,NULL,16);
				j+=2;
				size-=2;
				
				__ILWS_free(tmp1);
				break;
			};
			ql->next->data[i]=ch;
			j++;
		};
		ql->next->data[size]='\0';
		ret=ql->next->data; // to the last
		ql=ql->next;
		*iddb=*iddb+1;
		idf++;
 	};
	return ret;
}                                                                                          

/*
 * Function for Postdata
 */
char *__ILWS_Post(struct ClientInfo *ClientInfo, char *handle) {
	char *tmp1,*tmp2,*tmp3,*ret;
	struct _Post *pl=ClientInfo->PostList;
	char *defret="";
	int *iddb=NULL,*iddb2=NULL;
	int idf;
	int seek=1;
	size_t strsize;
	size_t size;
	int j=0,ch;
	unsigned int i;
	int rw=0; //return what;
	
	tmp1=strstr(current_web_client->rbuf,"Content-type: multipart/form-data"); // multipart this post doesn't work
	if(tmp1!=NULL) {
		return ClientInfo->MultiPart(handle).data;
	};
	if(handle==NULL) {
		return ClientInfo->PostData;
	};
	if(handle[0]=='#')rw=1;
	/* Allocate the list */
	if(ClientInfo->PostList==NULL) {
		if(!(ClientInfo->PostList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Post)))) {
			if(rw) return 0;
			return defret;
		};
		ClientInfo->PostList->next=NULL;
		ClientInfo->PostList->data=NULL;
		ClientInfo->PostList->id=NULL;
		pl=ClientInfo->PostList;
	};
	
	// First search if exists
	idf=1;
	iddb=&idf;
	seek=1;
	while(pl->next!=NULL) {
		if(pl->next->id!=NULL) {
			if(!strcmp(pl->next->id,handle+rw) && iddb>=0) {
				if(seek==1) {
					iddb=&pl->next->index;
					iddb2=&pl->next->idf;
					if(rw) return (char *)(*iddb2);
					if(pl->next->idf==1) {
						return pl->next->data;
					};
					j=*iddb;
					seek++;
				};
				*iddb=*iddb-1;
				
				if(*iddb<=0) {
					*iddb=j-1;
					if(j<=1) {
						*iddb=*iddb2;

						//return defret;
					};
					return pl->next->data;
				};
			};
		};
		pl=pl->next;	
	};
	
	
	
	
	
	/* Doesn't exists	 */
	strsize=strlen(handle+rw);
	tmp1=strstr(current_web_client->rbuf,"\r\n\r\n"); 
	if(tmp1!=NULL)
		tmp1+=4;
	else {
		if(rw) return 0;
		return defret;
	};
	idf=0;
	ret=defret;
	seek=1;
	tmp3=tmp1;
	while(seek==1) {
		tmp1=tmp3;
		do {
			tmp2=strstr(tmp1,handle+rw);
			if(tmp2==NULL) { // mustn't be null
				if(iddb!=NULL && iddb2!=NULL) { // if iddb2 is null then is just one value;
					*iddb2=*iddb;
					if(!rw)*iddb=*iddb-1;
				};
				if(rw) {
					if(ret==defret) return 0;
					return (char *)*iddb2;
				}
				return ret; // if first null return defret (ret=defret);
				
			};
			tmp1=tmp2+strsize;
		} while ((tmp2[-1]!='\n' && tmp2[-1]!='&') || tmp2[strsize]!='='); // Johannes E. Schindelin Fix
		tmp3=tmp1;

		
		pl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Post));
		if(pl->next==NULL) {
			if(rw) return 0;
			return defret;
		};
		pl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1);
		if(pl->next->id==NULL) {
			if(rw) return 0;
			return defret;
		};
		memcpy(pl->next->id,handle+rw,strsize);
		pl->next->id[strsize]=0;	
		if(idf==0) {
			pl->next->index=0;
			iddb=&pl->next->index;
			iddb2=&pl->next->idf;
		};

		pl->next->data=defret;
		pl->next->next=NULL;
							
		tmp1=strstr(tmp2,"&"); // goes to the next & (end of data)
		tmp2+=strsize+1;       // tmp2 goes to start of data
		if(tmp1==NULL) {
			size=strlen(tmp2);
		} else {
			size=tmp1-tmp2;
		};
		if(size==0) {
			if(rw) return 0;
			return defret;
		};
		
		pl->next->data=__ILWS_add_buffer(ClientInfo->mem,size+1);
		if(pl->next->data==NULL) {
			return defret;
		};
		j=0;
		for(i=0;i<size;i++) { // hex translation here
			switch (ch=tmp2[j]) {
				case '+':
					ch=' ';
					break;
				case '%':
					
					tmp1=__ILWS_malloc(3);             
					if(tmp1==NULL) {
						if(rw) return 0;
						return defret;
					};
					strncpy(tmp1,&tmp2[j+1],2);
					tmp1[2]=0;
					
					ch=strtol(tmp1,NULL,16);
					j+=2;
					size-=2;
					
					__ILWS_free(tmp1);
					break;
			};
			pl->next->data[i]=ch;
			j++;
		};
		pl->next->data[size]='\0';
		ret=pl->next->data; // to the last
		*iddb=*iddb+1;
		idf++;
		pl=pl->next;
		//pl->next->data=ret;
	};
	return ret;
}                                                        

/*
 * Function for MultiPart formdata
 */
struct _MultiPart __ILWS_MultiPart(struct ClientInfo *ClientInfo, char *handle) {
	char *tmp1,*tmp2,*tmp3;	
	int i;
	char *name;
	size_t namesize;
	struct _MultiPart *ml=ClientInfo->MultiPartList;
	struct _MultiPart defret={"","",0,""};
	size_t strsize;
	char *boundary; size_t boundarysize;
	// IE C43o6Fn6Et74e65n6Et74-2DT54y79p70e65:3A 20m6Du75l6Ct74i69p70a61r72t74/2Ff66o6Fr72m6D-2Dd64a61t74a61
	// NS C43o6Fn6Et74e65n6Et74-2Dt74y79p70e65:3A 20m6Du75l6Ct74i69p70a61r72t74/2Ff66o6Fr72m6D-2Dd64a61t74a61
	tmp1=__ILWS_stristr(current_web_client->rbuf,"Content-type: multipart/form-data");	
	if(tmp1==NULL) return defret;
	if(ClientInfo->MultiPartList==NULL) {
		ClientInfo->MultiPartList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _MultiPart));
		if(ClientInfo->MultiPartList==NULL) {
			return defret;
		};
		ClientInfo->MultiPartList->next=NULL;
		ClientInfo->MultiPartList->id=NULL;
		ClientInfo->MultiPartList->data=NULL;
		ClientInfo->MultiPartList->filename=NULL;
		ClientInfo->MultiPartList->size=0;
		ml=ClientInfo->MultiPartList;
	};
	// Check if handle exists
	while(ml->next!=NULL) {
		if(ml->next->id!=NULL) {
			if(!strcmp(ml->next->id,handle)) {
				
				return *ml->next;
			};
		};
		ml=ml->next;
	};	
	
	
	strsize=strlen(handle);
	ml->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _MultiPart));
	if(ml->next==NULL) {
		return defret;
	};
	ml->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1);
	if(ml->next->id==NULL) {
		return defret;
	};
	memcpy(ml->next->id,handle,strsize);
	ml->next->id[strsize]=0;
	ml->next->data="";
	ml->next->filename="";
	ml->next->size=0;
	ml->next->next=NULL;
	
	tmp1=strstr(tmp1,"boundary=");
	if(tmp1==NULL) return defret;
	tmp1+=9;
	tmp2=strstr(tmp1,"\r\n");
	if(tmp2<tmp1 || tmp2==NULL) return defret;
	/* boundary */
	boundarysize=tmp2-tmp1;
	boundary=__ILWS_add_buffer(ClientInfo->mem,boundarysize+3);
	if(boundary==NULL) {
		return defret;
	};
	memcpy(boundary,tmp1,boundarysize);
	boundary[boundarysize]=0;
	
	
	/* handle */	
	namesize=boundarysize+41+strlen(handle);
	name=__ILWS_add_buffer(ClientInfo->mem,namesize+1);
	if(name==NULL) {
		return defret;
	};
	snprintf(name,namesize,"%s\r\nContent-Disposition: form-data; name=",boundary);	
	namesize=strlen(name);
	
	tmp1=strstr(tmp1,"\r\n\r\n"); // go to data
	if(tmp1==NULL) return defret;
	
	do {
		i=memcmp(tmp1,name,namesize);	
		if(i==0) {
			tmp1+=namesize;
			if(tmp1[0]=='\"')tmp1+=1;
			if(strncmp(tmp1,handle,strlen(handle))){
				i=1;
			}else {
				if((tmp1[strsize]!=' ') && (tmp1[strsize]!='\"') && (tmp1[strsize]!='\r') && (tmp1[strsize]!=';') ) i=1;
			};

		}else { 
			tmp1+=1;
		};
	} while(i!=0 && (tmp1+namesize<current_web_client->rbuf+current_web_client->rbufsize)); // Search init of data
	if(i!=0) return defret;
	//tmp1+=namesize;
	tmp2=strstr(tmp1,"filename="); // get filename
	if(tmp2!=NULL) {
		tmp2+=9;
		if(tmp2[0]=='\"')tmp2+=1;
		tmp3=strstr(tmp2,"\r\n");
		ml->next->filename=__ILWS_add_buffer(ClientInfo->mem,(tmp3-tmp2)+1);
		if(ml->next->filename==NULL) {
			return defret;
		};
		memcpy(ml->next->filename,tmp2,tmp3-tmp2);
		ml->next->filename[tmp3-tmp2]='\0';
		if(ml->next->filename[tmp3-tmp2-1]=='\"')
			ml->next->filename[tmp3-tmp2-1]='\0';
		
	};
	tmp2=strstr(tmp1,"\r\n\r\n"); // data init
	if(tmp2==NULL)return defret;
	tmp2+=4;
	tmp3=tmp2;
	do {	
		
		i=memcmp(tmp3,boundary,boundarysize);  
		if(i!=0)tmp3+=1;
	} while(i!=0 && (tmp3+boundarysize<current_web_client->rbuf+current_web_client->rbufsize)); // End of data
	if(i!=0) return defret;
	tmp3-=4; // back "\r\n\r\n"

	// copy data to node	
	if(!(ml->next->data=__ILWS_add_buffer(ClientInfo->mem,(tmp3-tmp2)+1))) {
		return defret;
	};
	memcpy(ml->next->data,tmp2,tmp3-tmp2);
	ml->next->data[tmp3-tmp2]='\0';
	ml->next->size=tmp3-tmp2;
	
	
	
	
	return *ml->next;

};

/*
 * Function for CookieData
 */
char *__ILWS_Cookie(struct ClientInfo *ClientInfo, char *handle) {
	char *defret="";
	char *tmp1,*tmp2,*ret;
	int size;
	int strsize;
	struct _Cookie *cl=ClientInfo->CookieList;
	
	
	tmp1=strstr(current_web_client->rbuf,"\nCookie: "); // start of cookie string
	if(tmp1==NULL) { // no cookies
		return defret;
	};
	tmp1+=8;
	if(handle==NULL) {
		return ClientInfo->CookieString;
	};
	
	if(ClientInfo->CookieList==NULL) {
		
		ClientInfo->CookieList=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Cookie));
		if(ClientInfo->CookieList==NULL) {
			return defret;
		};
		ClientInfo->CookieList->next=NULL;
		ClientInfo->CookieList->data=NULL;
		ClientInfo->CookieList->id=NULL;
		cl=ClientInfo->CookieList;
	}
	// First search if exists
	while(cl->next!=NULL) {
		if(cl->next->id!=NULL) {
			if(!strcmp(cl->next->id,handle)) {
				
				return cl->next->data;
			};
		};
		cl=cl->next;	
	};
	
	strsize=strlen(handle);
	if(!(cl->next=__ILWS_add_buffer(ClientInfo->mem,sizeof(struct _Cookie)))) {
		return defret;
	};
	if(!(cl->next->id=__ILWS_add_buffer(ClientInfo->mem,strsize+1))) {
		return defret;
	};
	memcpy(cl->next->id,handle,strsize);
	cl->next->id[strsize]=0;
	cl->next->data=defret;
	cl->next->next=NULL;
	do {
		tmp2=strstr(tmp1,handle);
		if(tmp2==NULL) {
			return defret;
		}else if(tmp2[strsize]==';' && tmp2[-1]==' ') {
			cl->next->data=__ILWS_add_buffer(ClientInfo->mem,6);
			snprintf(cl->next->data,5,"True");
			return cl->next->data;
		};
		tmp1=tmp2+strsize;
	}while(tmp2[-1]!=' ' || tmp2[strsize]!='=');
	
	tmp1=strstr(tmp2,";"); // end of data
	tmp2+=strsize+1;     // start of data
	if(tmp1==NULL) {
		size=strstr(tmp2,"\r")-tmp2;
		
	} else {
		size=tmp1-tmp2;
	};
	if(size<1) {
		return defret;
	};
	
	ret=__ILWS_add_buffer(ClientInfo->mem,size+1);
	if(ret==NULL) {
		return defret;
	};
	
	memcpy(ret,tmp2,size);
	ret[size]='\0';
	cl->next->data=ret;
	return cl->next->data;	
};

/*
 * get whole query string
 */
char *__ILWS_clientinfo_getquerystring(struct ClientInfo *ClientInfo) {
	char *tmp1,*tmp2,*ret;
	char *defret="";
	size_t size;
        tmp1=strstr(current_web_client->rbuf,"?"); 
        tmp2=strstr(current_web_client->rbuf,"HTTP");
        if(tmp1!=NULL && tmp1<tmp2)
                tmp1+=1;
        else
                return defret;
	size=(tmp2-tmp1)-1;
	ret=__ILWS_add_buffer(ClientInfo->mem,size+1);
	if(ret==NULL) {
		return defret;
	};
	memcpy(ret,tmp1,size);
	ret[size]=0;
	return ret;
};

/*
 * get whole post data
 */ 
char *__ILWS_clientinfo_getpostdata(struct ClientInfo *ClientInfo) {
	char *tmp1,*ret;
	char *defret="";
	size_t size;
	tmp1=strstr(current_web_client->rbuf,"\r\n\r\n"); 
	if(tmp1!=NULL && (tmp1+4)<(char*)(current_web_client->rbuf+current_web_client->rbufsize))
		tmp1+=4;
	else
		return defret;
	size=(current_web_client->rbuf+current_web_client->rbufsize)-tmp1;
	ret=__ILWS_add_buffer(ClientInfo->mem,size+1);
	if(ret==NULL) {
		return defret;
	};
	memcpy(ret,tmp1,size);
	ret[size]='\0';
	return ret;
}

/*
 * get method (GET POST HEAD etc)
 */
char *__ILWS_clientinfo_getmethod(struct ClientInfo *ClientInfo) {
	char *tmp1,*ret;
	char *defret="";
	size_t size;
	tmp1=strstr(current_web_client->rbuf," "); // first space
	if(tmp1==NULL) {
		return defret;
	};
	size=tmp1-current_web_client->rbuf;
	ret=__ILWS_add_buffer(ClientInfo->mem,size+1);
	if(ret==NULL) {
		return defret;
	};
	memcpy(ret,current_web_client->rbuf,size);
	ret[size]=0;
	return ret;
}

/*
 * get request name (GET /taltal HTTP/1.0) returning /taltal
 */
char *__ILWS_clientinfo_getreqname(struct ClientInfo *ClientInfo) {
	char *ret;
	char *tmp1=strstr(current_web_client->rbuf,"/"); // Must have /
	char *tmp2=strstr(tmp1,"?");
	char *tmp3=strstr(tmp1," HTTP");
	char *defret="";
	size_t i,j;
	int ch;
	size_t size=0;
	if(tmp1==NULL || tmp3==NULL) return defret;
	if(tmp2==NULL || tmp2>tmp3) {
		tmp2=tmp3;
	};
	//tmp1+=1;
	size=tmp2-tmp1;
	if(size<1) 
		return defret;
	ret=__ILWS_add_buffer(ClientInfo->mem,size+1);
	if(ret==NULL) {
		return defret;
	};
	j=0;
	for(i=0;i<size;i++) { // hex translation here
		switch (ch=tmp1[j]) {
			case '+':
				ch=' ';
				break;
			case '%':
				
				tmp2=__ILWS_malloc(3);             
				if(tmp2==NULL) {
					return defret;
				};
				strncpy(tmp2,&tmp1[j+1],2);
				tmp2[2]=0;
				
				ch=strtol(tmp2,NULL,16);
				j+=2;
				size-=2;
				__ILWS_free(tmp2);
				break;
		};
		ret[i]=ch;
		j++;
	};
	//pl->next->data[size]='\0';
	//memcpy(ret,tmp1,size);
	ret[size]=0;
	return ret;
}