/*
 * This code is part of Screws project.
 * Copylefted by pancake@phreaker.net at 2003
 */

#include "main.h"
#include "Env.h"

void screws_heads_parse(s,b,head,nheads)
	int s;
	char *b;
	char **head;
	int nheads;
{
	int i,j,nh;
	bool multipart=false;
	long clength=0;

	/* Init global vars */
	if (CLENGTH) free(CLENGTH); CLENGTH=0;

	if (CONNECTION) free(CONNECTION); CONNECTION=0;
	for(i=j=0,nh=1;nh<=nheads;i++)
	{
		switch(b[i])
		{
		case '\r':
			b[i]=0;
			if (Svr.v>=V_SHOW)
			  printf("=(%d|%d)=> '%s'\n",nh,nheads,b+j);

			head[nh]=strdup(b+j);

			if (!strncmp("Content-Type: ",b+j,14))
			{
				if (!strncmp(b+j+14,"multipart/form-data;",20))
				{
				printf("Uplading files!! alert!! x''DD ai q ploro\n");
				multipart=true;
				}
				// si es multipart/form-data poden pujar fitxers!! wow xD
				// name="variable"
				// filename="filename"
				// Content-Type: text/html
				// Content-Length: 42242
				// TODO upload files
				//printf("CONTENT IS (%s)\n",b+j+14);
			} else
			if (!strncmp("Content-Disposition: ",b+j,21))
			{
				printf("DISPOSITION IS (%s)\n",b+j+21);
			} else
			if (!strncmp("Range: bytes=",b+j,13))
			{
				char *range=b+j+13;
				char *rseek,*token;
				char buf[1024];
				long len,seek;

				// FORMAT IS : number- <- seek number fins al final
				// FORMAT IS : 0-999,9500- <- seek=9500, len=1000
				setenv("RANGE",b+j+13,1);

			   token=strchr(range,',');
			   len=0;
   			if (token)
			   {
					token[0]=0;
#if 0
// TODO range-bytes. must take comma separated ranges. f* rfc!
			      token[0]=0;
			      rseek=token+1;

			      token=strchr(range,'-');
			      if (token)
			      {
			         token[0]=0;
			         len=atoi(token+1)-atoi(range)+1;
			      }
#endif
			   } 

				rseek=range;
				token=strchr(range,'-');
				if (token)
				{
					token[0]=0;
					len=atoi(token+1)-atoi(range)+1;
				}

   			seek=atoi(rseek);
			   if (seek<0) seek=0;
			   if (len<0) len=0;

				// RANGE_LEN
				// RANGE_SEEK
				sprintf(buf,"%d",seek);
				setenv("RANGE_SEEK",buf,1);
				sprintf(buf,"%d",len);
				setenv("RANGE_LEN",buf,1);
			} else
			if (!strncmp("Host: ",b+j,6))
			{
				if (HOST) free(HOST);
				else HOST=strdup(b+j+6);
			} else 
			if (!strncmp("Accept-Language: ",b+j,17))
			{	setenv("ACCEPT_LANGUAGE",b+j+17,1);
			} else
			if (!strncmp("Accept: ",b+j,8))
			{  setenv("ACCEPT",b+j+8,1);
			} else
			if (!strncmp("Accept-Encoding: ",b+j,17))
			{ setenv("ACCEPT_ENCODING",b+j+17,1);
			} else
			if (!strncmp("User-Agent: ",b+j,12))
			{
				if (USERAGENT) free(USERAGENT);
				else USERAGENT=strdup(b+j+12);
			} else
			if (!strncmp("Referer: ",b+j,9))
			{
				if (REFERER) free(REFERER);
				else REFERER=strdup(b+j+9);
			} else
			if (!strncmp("Connection: ",b+j,12))
			{
				if (CONNECTION) free(CONNECTION);
				else CONNECTION=strdup(b+j+12);
			} else
			if (!strncmp("Content-Length: ",b+j,16))
			{
				CLENGTH=strdup(b+j+16);
				clength=atoi(CLENGTH);
			}
		// TODO ADD MODULE_HEADER TYPE
			j=i+1;
			nh++;
			break;
		case '\n':
			b[i]=0;
			break;
		}
	}
	head[nh]=0;

	/* Content-Length code (POST Like) */
	if (CLENGTH)
	{
		char *content=0;

		if (clength<1)
		{
			fprintf(stderr,"Invalid value in Clength '%s'\n",CLENGTH);
			/* ERROR */
		} else {
			/* Read Da Content And Append It To Args */
			content = (char *)malloc(1+clength);
			screws_socket_recv(s,content,clength);

			ARGS=(char *)realloc(ARGS,strlen(ARGS)+clength+1);
			strcat(ARGS,content);
			CONTENT=content;
		}
	}

	/* multipart support (uploading files) */
	if (multipart)
	{
		char *end;
		char *filename=0;
		int tokenlen=0;
		char *token=0;
		char *filesizeptr=0;
		long int filesize=0;
		char *varname=0;
		char *file=0;
		char *begin=strstr(CONTENT,"\r\n\r\n"); // XXX BUGGY NOT4bins
		if (begin)
		{
			begin[0]=0;
			end=strstr(CONTENT,"\r\n");
			if (end)
			{
				end[0]=0;
				tokenlen=strlen(CONTENT)+6;
				token=strdup(CONTENT);
				printf("tokenstring(%s)(%d)\n",CONTENT,strlen(CONTENT));
				end[0]='\r';
			}
			begin[0]='\r';
			/* take header information */
			// Content-Disposition: form-data;
			// name="anan";
			// filename="file";
			// ctype="xx";
			// TODO must use a function that strdups this as return!
			
			filename=strstr(CONTENT,"filename=\"");
			if (filename)
			{
				filename=filename+10;
				end=strstr(filename,"\"");
				end[0]=0;
				filename=strdup(filename);
				end[0]='\"';
				printf("FILENAME IS (%s)\n",filename);
			}

			varname=strstr(CONTENT,"name=\"");
			if (varname)
			{
				varname=varname+6;
				end=strstr(varname,"\"");
				end[0]=0;
				varname=strdup(varname);
				end[0]='\"';
				printf("VARNAME IS (%s)\n",varname);
			}
			/* copy data into memory */
			begin+=4;
		printf("BEIGN(%s)\n",begin);
		printf("FOUND(%s)\n",memmem(begin,strlen(begin),token,strlen(token)));
		
			filesize= clength-(begin-CONTENT)-tokenlen; 
			printf("FILESIZE(%d)\n",filesize);
			file=(char*)calloc(1,filesize+1);
			memcpy(file,begin,filesize);
		}
	}
}
