			/* ------------------------------------- */
			//										 //
			//  +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+  //
			//  |S|i|m|p|l|e| |P|r|o|x|y| |2|0|0|4|  //
			//  +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+  //
			//										 //
			//										 //
			//  AUTOR : Aymeric  //
			//  DATE  : May 2004					 //
			//  FILE  : readHTTP.c					 //
			//  DESC  : functions to treat headers   //
			//          HTTP of the message			 //
			//  									 //
			/* ------------------------------------- */


/************************************************************************/

#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "proxy.h"

/************************************************************************/
// FunctionName : readHTTPHeader
//
// Description  : read the important headers of the message
//
// Parameters
// [in]
//			char *message			   : the message to treat
//			struct headerHTTP *Headers : contain the important headers
//			struct sLog *alog		   : contain connected hostname
//
// [out]
//			struct headerHTTP *Headers : contain the important headers
//			struct sLog *alog		   : contain hostname and request firstline
//
// Returns
//			char * : a message to indicate if it's ok or not
//
// Calls
//			init()				: initialise struct headerHTTP
//			checkIfRequest()	: return true if it's a request
//			checkIfResponse()	: return true if it's a response
//			checkRequestLine()	: return true if the firstline is good
//			checkResponseLine() : return true if the firstline is good
//			getRequestHeader()  : look at the request headers and set them
//			getResponseHeader() : look at the response headers and set them
//			getGeneralHeader()  : look at the general headers and set them
//			getEntityHeader()   : look at the entity headers and set them
//
// Globals
//			FILE *log	 	 : file id for the general log
//			enum bool debug	 : if debug is true, we are in debug mode
//
/************************************************************************/

char* readHTTPHeader(char *message, struct headerHTTP *Headers, struct sLog *alog)
{
	char *msg;
    char sep[]="\r\n";
    char *ptr,*ptr2;
    char *firstline;
	char *secondline;
	char *fromsecondline;
	char c='\n';
	int i=0,j;


    struct headerHTTP importantHeaders;
	init(&importantHeaders);

	/* i is the length of the firstline */
	while ( message[i] != '\n' || message[i+1] != '\r' ) i++;

	msg=(char *)calloc(sizeof(char),i+1);

	for (j=0; j < i; j++) msg[j]=message[j];

	/* each line is separated by '\r\n' */
	if (debug) printf("[readHTTPHeader] New message received...\n");
	//if (debug) printf("%s\n",message);

    ptr = strtok(msg, sep);

	/* ptr = first line */
	if ( ptr == NULL )	return("FirstLineEmpty");

	firstline=(char *)calloc(sizeof(char),strlen(ptr)+1);
	strcpy(firstline,ptr);

	alog->firstline=(char *)calloc(sizeof(char),strlen(ptr)+1);
	strcpy(alog->firstline,ptr);

	if (debug) printf("[readHTTPHeader] First line : %s\n",firstline);


	/* we determine the message from the second line */
	if ( (ptr2 = strchr(message, c)) != NULL )
	if (ptr2)
	{
		int i;
		fromsecondline=message;
		for (i=0; i < (ptr2-message); i++) *fromsecondline++;
		*fromsecondline++;
	}

	/* we keep the second line to check if it's not empty */
	ptr = strtok(NULL, sep);
	if ( ptr == NULL ) return("SecondLineEmpty");

	secondline=(char *)calloc(sizeof(char),strlen(ptr)+1);
	strcpy(secondline,ptr);


	if ( checkIfRequest(firstline) )
	{
		/* REQUEST MESSAGE */

		if ( !checkRequestLine(firstline,&importantHeaders) ) return("badRequestLine");

		getRequestHeader(&importantHeaders, fromsecondline);

		getGeneralHeader(&importantHeaders, fromsecondline);

		getEntityHeader(&importantHeaders, fromsecondline);

		Headers->URIproto=(char *)calloc(sizeof(char),strlen(importantHeaders.URIproto)+1);
		strcpy(Headers->URIproto,importantHeaders.URIproto);

		Headers->URIserv=(char *)calloc(sizeof(char),strlen(importantHeaders.URIserv)+1);
		strcpy(Headers->URIserv,importantHeaders.URIserv);

		Headers->URIpath=(char *)calloc(sizeof(char),strlen(importantHeaders.URIpath)+1);
		strcpy(Headers->URIpath,importantHeaders.URIpath);

		Headers->URIport=importantHeaders.URIport;

		if ( importantHeaders.Host != NULL )
		{
			Headers->Host=(char *)calloc(sizeof(char),strlen(importantHeaders.Host)+1);
			strcpy(Headers->Host,importantHeaders.Host);
		}
		if ( importantHeaders.CacheControl != NULL )
		{
			Headers->CacheControl=(char *)calloc(sizeof(char),strlen(importantHeaders.CacheControl)+1);
			strcpy(Headers->CacheControl,importantHeaders.CacheControl);
			if ( importantHeaders.MaxAge != 0 ) Headers->MaxAge=importantHeaders.MaxAge;
			free(importantHeaders.CacheControl);
		}
		if ( importantHeaders.Pragma != NULL )
		{
			Headers->Pragma=(char *)calloc(sizeof(char),strlen(importantHeaders.Pragma)+1);
			strcpy(Headers->Pragma,importantHeaders.Pragma);
			free(importantHeaders.Pragma);
		}

		Headers->Date=importantHeaders.Date;

		Headers->Expires=importantHeaders.Expires;

		Headers->ContentLength=importantHeaders.ContentLength;

		return("requestMessage");
	}

	if ( checkIfResponse(firstline) )
	{
		/* RESPONSE MESSAGE */

		if ( !checkResponseLine(firstline,&importantHeaders) ) return("badResponseLine");

		getResponseHeader(&importantHeaders, fromsecondline);

		getGeneralHeader(&importantHeaders, fromsecondline);

		getEntityHeader(&importantHeaders, fromsecondline);

		Headers->Age=importantHeaders.Age;

		if ( importantHeaders.CacheControl != NULL )
		{
			Headers->CacheControl=(char *)calloc(sizeof(char),strlen(importantHeaders.CacheControl)+1);
			strcpy(Headers->CacheControl,importantHeaders.CacheControl);
			if ( importantHeaders.MaxAge != -1 ) Headers->MaxAge=importantHeaders.MaxAge;
			free(importantHeaders.CacheControl);
		}
		if ( importantHeaders.Pragma != NULL )
		{
			Headers->Pragma=(char *)calloc(sizeof(char),strlen(importantHeaders.Pragma)+1);
			strcpy(Headers->Pragma,importantHeaders.Pragma);
			free(importantHeaders.Pragma);
		}

		Headers->Date=importantHeaders.Date;

		Headers->Expires=importantHeaders.Expires;

		Headers->ContentLength=importantHeaders.ContentLength;

		Headers->StatusCode=importantHeaders.StatusCode;

		return("responseMessage");
	}

	return("badMessage");
}

/************************************************************************/
// FunctionName : checkIfRequest
//
// Description  : check if the message is a Request
//
// Parameters
// [in]
//			char *first_line : the firstline of the message
//
// [out]
//			nothing
//
// Returns
//			bool : true if it's a request message
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

enum bool checkIfRequest(char * first_line)
{
    char sep[]=" ";
    char msg[strlen(first_line)];

    char *method;

	strcpy(msg,first_line);
    method = strtok(msg, sep);

    /* We look at the method used */
    if ( method != NULL ) {
		if ((strcmp(method,"HEAD")  != 0) && (strcmp(method,"GET") != 0) && (strcmp(method,"PUT") != 0) &&
		    (strcmp(method,"OPTIONS") != 0) && (strcmp(method,"POST") != 0) && (strcmp(method,"DELETE") != 0) &&
		    (strcmp(method,"TRACE") != 0) && (strcmp(method,"CONNECT") != 0))
			return(false);
		else return(true);
	}
	else return(false);
}


/************************************************************************/
// FunctionName : checkIfResponse
//
// Description  : check if the message is a Response
//
// Parameters
// [in]
//			char *first_line : the firstline of the message
//
// [out]
//			nothing
//
// Returns
//			bool : true if it's a response message
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

enum bool checkIfResponse(char *first_line)
{
	char sep[]=" ";
	char msg[strlen(first_line)];

	char *HTTPversion;

	strcpy(msg,first_line);
	HTTPversion = strtok(msg, sep);

	if ( HTTPversion != NULL )
		if ((strcmp(HTTPversion,"HTTP/1.0") != 0) && (strcmp(HTTPversion,"HTTP/1.1") != 0))
			return(false);
	return(true);
}

/************************************************************************/
// FunctionName : checkRequestLine
//
// Description  : check if the firstline is a correct request line and
//				  set the parameters
//
// Parameters
// [in]
//			char *first_line : the firstline of the message
//			headerHTTP *importantHeaders : contain the important headers
//
// [out]
//			headerHTTP *importantHeaders : contain the important headers
//
// Returns
//			bool : true if the requestline is good
//
// Calls
//			nothing
//
// Globals
//			enum bool debug	 : if debug is true, we are in debug mode
//
/************************************************************************/

enum bool checkRequestLine(char *firstline, struct headerHTTP *importantHeaders)
{
	char sep[]=" ";
	char msg[strlen(firstline)];

	char *method;
	char *URI;
	char *HTTPversion;

	char tempo[BUFFERSIZE];
	int i;

	struct headerHTTP temp;

	strcpy(msg,firstline);

	method = strtok(msg, sep);
	if ( method == NULL ) return(false);
	URI = strtok(NULL, sep);
	if ( URI == NULL ) return(false);
	HTTPversion = strtok(NULL, sep);
	if ( HTTPversion == NULL ) return(false);

	/* only GET, HEAD, PUT and POST are allowed */
	if ((strcmp(method,"GET") != 0) && (strcmp(method,"HEAD") != 0)  && (strcmp(method,"PUT") != 0) && (strcmp(method,"POST") != 0))
	{
		if (debug) printf("[checkRequestLine] Invalid method (%s)\n",method);
		return(false);
	}

	init(&temp);
	if ( getDetailsURI(&temp,URI) == false ) return(false);

	if (debug)
	{
		if ( temp.URIproto != NULL ) printf("[checkRequestLine] URIproto = %s\n",temp.URIproto);
		if ( temp.URIserv != NULL ) printf("[checkRequestLine] URIserv = %s\n",temp.URIserv);
		if ( temp.URIpath != NULL ) printf("[checkRequestLine] URIpath = %s (%d)\n",temp.URIpath,strlen(temp.URIpath));
		if ( temp.URIport != 0 ) printf("[checkRequestLine] URIport = %d\n",temp.URIport);
	}

	for (i=0;i<strlen(temp.URIpath);i++)
	{
		tempo[i]=temp.URIpath[i];
		tempo[i+1]='\0';
	}

	importantHeaders->URIproto=(char *)calloc(sizeof(char),strlen(temp.URIproto)+1);
	strcpy(importantHeaders->URIproto,temp.URIproto);

	importantHeaders->URIport=temp.URIport;

	importantHeaders->URIserv=(char *)calloc(sizeof(char),strlen(temp.URIserv)+1);
	strcpy(importantHeaders->URIserv,temp.URIserv);

	printf("[checkRequestLine] troublon URIpath = %s (%d)\n",tempo,strlen(tempo));
	importantHeaders->URIpath=(char *)calloc(sizeof(char),strlen(tempo)+1);
	//strcpy(importantHeaders->URIpath,temp.URIpath);
	strcpy(importantHeaders->URIpath,tempo);
	if (debug) printf("[checkRequestLine] URIpath=%s\n",importantHeaders->URIpath);

	importantHeaders->HTTPversion=(char *)calloc(sizeof(char),strlen(HTTPversion)+1);
	strcpy(importantHeaders->HTTPversion,HTTPversion);

	/* only HTTP/1.0 and HTTP/1.1 allowed */
	if ((strcmp(HTTPversion,"HTTP/1.0") != 0) && (strcmp(HTTPversion,"HTTP/1.1") != 0))
	{
		if (debug) printf("[checkRequestLine] Invalid HTTP version (%s)\n",HTTPversion);
		return(false);
	}

	return(true);
}

/************************************************************************/
// FunctionName : getDetailsURI
//
// Description  : look into the URI and set the parameters
//
// Parameters
// [in]
//			struct headerHTTP *Headers : to set the values
//
//
// [out]
//			nothing
//
// Returns
//			bool : true if no problem
//
// Calls
//			nothing
//
// Globals
//			enum bool debug	 : if debug is true, we are in debug mode
//
/************************************************************************/

enum bool getDetailsURI(struct headerHTTP *Headers, char *requestURI)
{
	char sep[]=":/";
	char *ptr,*svg;
	int setPort=0;
	int i,j=0;

	svg=(char*)calloc(sizeof(char),strlen(requestURI)+1);
	strcpy(svg,requestURI);

	ptr = strtok(requestURI, sep);

	if ( ptr != NULL )
	{
		if ( strcmp(ptr,"http") != 0 )
		{
			if (debug) printf("[getDetailsURI] Invalid protocol (%s)\n",ptr);
			return(false);
		}
		else
		{
			Headers->URIpath=NULL;
			Headers->URIproto=(char *)calloc(sizeof(char),strlen(ptr)+1);
			strcpy(Headers->URIproto,ptr);

			ptr = strtok(NULL, sep);
			if ( ptr != NULL )
			{
				Headers->URIserv = (char *)calloc(sizeof(char),strlen(ptr)+1);
				strcpy(Headers->URIserv,ptr);
			}

			ptr = strtok(NULL, sep);

			if ( ptr != NULL )
			{
				if ( is_number(ptr) )
				{
					Headers->URIport = atoi(ptr);
					setPort=1;
					ptr = strtok(NULL, sep);
				}
			}

			if ( ptr != NULL )
			{
				printf("\n[PouetPouet] svg=%s et lenURI=%d ptr-requestURI=%d",svg,strlen(svg),ptr-requestURI);
				Headers->URIpath=(char *)calloc(sizeof(char),strlen(svg)-(int)(ptr-requestURI)+1+1);
				j=0;
				printf("\n[ProutProut] ");
				for (i=(int)(ptr-requestURI-1);i<strlen(svg);i++)
				{
					printf("%c",svg[i]);
					Headers->URIpath[j]=svg[i];
					Headers->URIpath[j+1]='\0';
					j++;
				}
				printf("\n[ProutProutProutProut] i=%d j=%d total=%d",i,j,strlen(svg)-(int)(ptr-requestURI)+1+1);

			}
			else
			{
				Headers->URIpath=(char*)malloc(2);
				strcpy(Headers->URIpath,"/");
			}
		}
	}
	if ( setPort == 0 ) Headers->URIport = 80;
	if (debug) printf("\n[getDetails] URIpath = %s\n",Headers->URIpath);
	return(true);
}

/*
enum bool getDetailsURI(struct headerHTTP *Headers, char *requestURI)
{
	char sep[]=":/";
	char *ptr;
	int setPort=0;
	int add=0; // determine if we need to add a /

	if ( requestURI[strlen(requestURI)-1] == '/' ) add=1;

	ptr = strtok(requestURI, sep);

	if ( ptr != NULL )
	{
		if ( strcmp(ptr,"http") != 0 )
		{
			if (debug) printf("[getDetailsURI] Invalid protocol (%s)\n",ptr);
			return(false);
		}
		else
		{
			Headers->URIpath=NULL;
			Headers->URIproto=(char *)calloc(sizeof(char),strlen(ptr)+1);
			strcpy(Headers->URIproto,ptr);

			ptr = strtok(NULL, sep);
			if ( ptr != NULL )
			{
				Headers->URIserv = (char *)calloc(sizeof(char),strlen(ptr)+1);
				strcpy(Headers->URIserv,ptr);
			}

			ptr = strtok(NULL, sep);

			if ( ptr != NULL )
			{
				if ( is_number(ptr) )
				{
					Headers->URIport = atoi(ptr);
					setPort=1;
				}
				else
				{
					Headers->URIpath=(char *)calloc(sizeof(char),strlen(ptr)+1+1);
					strcat(Headers->URIpath,"/");
					strcat(Headers->URIpath,ptr);
				}
			}

			ptr = strtok(NULL, sep);
			if ( ptr != NULL )
			{
				do
				{
					if ( Headers->URIpath == NULL ) Headers->URIpath=(char *)calloc(sizeof(char),1+1);
					else (char *)realloc(Headers->URIpath,sizeof(char)*(strlen(Headers->URIpath)+1+1));
					strcat(Headers->URIpath,"/");
					(char *)realloc(Headers->URIpath,sizeof(char)*(strlen(ptr)+1+strlen(Headers->URIpath)));
					strcat(Headers->URIpath,ptr);
					ptr = strtok(NULL, sep);
				} while ( ptr != NULL );
			}
			if (Headers->URIpath == NULL)
			{
				Headers->URIpath = (char *)malloc(2);
				if (Headers->URIpath) strcpy(Headers->URIpath, "/");
			}
			else if (add == 1)
			{
				(char *)realloc(Headers->URIpath,sizeof(char)*(strlen(Headers->URIpath)+2));
				strcat(Headers->URIpath,"/");
			}
		}
	}
	if ( setPort == 0 ) Headers->URIport = 80;
	if (debug) printf("[getDetails] URIpath = %s\n",Headers->URIpath);
	return(true);
}
*/

/************************************************************************/
// FunctionName : is_number
//
// Description  : check if the string in param is a number
//
// Parameters
// [in]
//			char *s : the string to check
//
// [out]
//			nothing
//
// Returns
//			bool : true if the string is a number
//
// Calls
//			isdigit() : return true if the character is a digit
//
// Globals
//			none
//
/************************************************************************/

enum bool is_number(char *s) {

	while (*s != '\0')
	{
		if(! isdigit(*s)) return(false);
		s++;
	}
	return(true);
}

/************************************************************************/
// FunctionName : getRequestHeader
//
// Description  : set the important request headers
//
// Parameters
// [in]
//			struct headerHTTP *Headers : contain important headers
//			char *line				   : the message
//
// [out]
//			struct headerHTTP *Headers : with new important headers
//
// Returns
//			nothing
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

void getRequestHeader(struct headerHTTP *Headers, char *line)
{
	char sep[]=" \r\n";
	char msg[strlen(line)];
	char *ptr;

	strcpy(msg,line);

	ptr = strtok(msg, sep);

	while (ptr != NULL)
	{
		if ( strcmp(ptr,"Host:") == 0 )
		{
			ptr = strtok(NULL,sep);
			if ( ptr != NULL )
			{
				Headers->Host=(char *)calloc(sizeof(char),strlen(ptr));
				strcpy(Headers->Host,ptr);
			}
			break;
		}
		ptr = strtok(NULL,sep);
	}
}


/************************************************************************/
// FunctionName : getGeneralHeader
//
// Description  : set the important general headers
//
// Parameters
// [in]
//			struct headerHTTP *Headers : contain important headers
//			char *line				   : the message
//
// [out]
//			struct headerHTTP *Headers : with new important headers
//
// Returns
//			nothing
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

void getGeneralHeader(struct headerHTTP *Headers, char *line)
{
	char sep[]=" \r\n";
	char msg[strlen(line)];
	char *ptr;

	strcpy(msg,line);

	ptr = strtok(msg, sep);

	while (ptr != NULL)
	{
		if ( strcmp(ptr,"Cache-Control:") == 0 )
		{
			ptr = strtok(NULL,", \r\n");
			if ( ptr != NULL )
			{
				Headers->CacheControl=(char *)calloc(sizeof(char),strlen(ptr)+1);
				strcpy(Headers->CacheControl,ptr);
				if ( strcmp(Headers->CacheControl,"max-age") == 0 )
				{
					ptr = strtok(NULL,"= ,\r\n");
					if ( ptr != NULL ) Headers->MaxAge=atoi(ptr);
				}
			}
		}
		else if ( strcmp(ptr,"Pragma:") == 0 )
		{
			ptr = strtok(NULL,sep);
			if ( ptr != NULL )
			{
				Headers->Pragma=(char *)calloc(sizeof(char),strlen(ptr)+1);
				strcpy(Headers->Pragma,ptr);
			}
		}
		else if ( strcmp(ptr,"Date:") == 0 )
		{
			ptr = strtok(NULL,"\r\n");
			if ( ptr != NULL )
				Headers->Date=strtotime("%a, %d %b %Y %H:%M:%S GMT",ptr);
		}

		ptr = strtok(NULL,sep);
	}
}

/************************************************************************/
// FunctionName : getEntityHeader
//
// Description  : set the important entity headers
//
// Parameters
// [in]
//			struct headerHTTP *Headers : contain important headers
//			char *line				   : the message
//
// [out]
//			struct headerHTTP *Headers : with new important headers
//
// Returns
//			nothing
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

void getEntityHeader(struct headerHTTP *Headers, char *line)
{
	char sep[]=" \r\n";
	char msg[strlen(line)];
	char *ptr;

	strcpy(msg,line);

	ptr = strtok(msg, sep);

	while (ptr != NULL)
	{
		if ( strcmp(ptr,"Expires:") == 0 )
		{
			ptr = strtok(NULL,"\r\n");
			if ( ptr != NULL )
				Headers->Expires=strtotime("%a, %d %b %Y %H:%M:%S GMT",ptr);
		}
		else if ( strcmp(ptr,"Content-Length:") == 0 )
		{
			ptr = strtok(NULL,"\r\n");
			if ( ptr != NULL )
				Headers->ContentLength=atoi(ptr);
		}
		ptr = strtok(NULL,sep);
	}
}

/************************************************************************/
// FunctionName : checkResponseLine
//
// Description  : check if the firstline is a correct response line and
//				  set the parameters
//
// Parameters
// [in]
//			char *first_line : the firstline of the message
//			headerHTTP *importantHeaders : contain the important headers
//
// [out]
//			headerHTTP *importantHeaders : contain the important headers
//
// Returns
//			bool : true if the responseline is good
//
// Calls
//			nothing
//
// Globals
//			enum bool debug	 : if debug is true, we are in debug mode
//
/************************************************************************/

enum bool checkResponseLine(char *line, struct headerHTTP *H)
{
	char sep[]=" ";
	char *ptr;
	int StatusCode;

	ptr = strtok(line, sep);
	if ( ptr != NULL ) ptr = strtok(NULL, sep);

	if ( ptr == NULL ) return(false);

	StatusCode = atoi(ptr);

	if ( (StatusCode < 100 && StatusCode > 101) ||
		 (StatusCode < 200 && StatusCode > 206) ||
		 (StatusCode < 300 && StatusCode > 307 && StatusCode != 306) ||
		 (StatusCode < 400 && StatusCode > 417) ||
		 (StatusCode < 500 && StatusCode > 505) )
	{
		if (debug) printf("[checkResponseLine] Invalid Status Code (%d)\n",StatusCode);
		return(false);
	}

	H->StatusCode=StatusCode;
	return(true);
}

/************************************************************************/
// FunctionName : getResponseHeader
//
// Description  : set the important response headers
//
// Parameters
// [in]
//			struct headerHTTP *Headers : contain important headers
//			char *line				   : the message
//
// [out]
//			struct headerHTTP *Headers : with new important headers
//
// Returns
//			nothing
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

void getResponseHeader(struct headerHTTP *Headers, char *line)
{
	char sep[]=" \r\n";
	char msg[strlen(line)];
	char *ptr;

	strcpy(msg,line);

	ptr = strtok(msg, sep);

	while (ptr != NULL)
	{
		if ( strcmp(ptr,"Age:") == 0 )
		{
			ptr = strtok(NULL,sep);
			if ( ptr != NULL )
			{
				Headers->Age=atoi(ptr);
			}
			break;
		}
		ptr = strtok(NULL,sep);
	}
}

/************************************************************************/
// FunctionName : strtotime
//
// Description  : convert a char* into a time_t
//
// Parameters
// [in]
//			char *format : the format of the result
//			char *date	 : what we need to change
//
// [out]
//			nothing
//
// Returns
//			time_t : the result of the convertion
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

time_t strtotime(char *format, char *date)
{
	time_t auxtime=0;
	struct tm *time_rec;

	while ((*date==' ') && (*date!=0))
		date++;

	if (strlen(date)==0) return 0;

	auxtime=time(0);
	time_rec=localtime(&auxtime);
	time_rec->tm_sec=0;
	time_rec->tm_min=0;
	time_rec->tm_hour=0;

	if(strptime(date,format,time_rec)==NULL) return -1;

	return mktime(time_rec);
}

