			/* ------------------------------------- */
			//										 //
			//  +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+  //
			//  |S|i|m|p|l|e| |P|r|o|x|y| |2|0|0|4|  //
			//  +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+  //
			//										 //
			//										 //
			//  AUTOR : Aymeric  //
			//  DATE  : May 2004					 //
			//  FILE  : cache.c						 //
			//  DESC  : functions to determine if    //
			//          we can cache or not			 //
			//  									 //
			/* ------------------------------------- */


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

#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 : CacheControlProcedure
//
// Description  : treat specification of the CacheControl header
//
// Parameters
// [in]
//			struct sCache *C : used to define if the cache is activated
//			char *CC		 : CacheControl parameter found before
//			time_t maxage	 : the maxage found before
//			bool response	 : true if it's a response message
// [out]
//			strutc sCache 	 : C->cache is true if we can cache the response
//
// Returns
//			char *	: "OK" means do nothing special
//					  "getNotFromCache" means we must not get from cache
//					  "getFromCache" means we can get from cache
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

char *CacheControlProcedure(struct sCache *C, char *CC, time_t maxage,
							enum bool response)
{
	if (strcmp(CC,"public") == 0)
	{
		C->cache=true;
		return("OK");
	}
	if ( (strcmp(CC,"private") == 0) || (strcmp(CC,"no-store") == 0) )
	{
		C->cache=false;
		return("OK");
	}
	if ( (response) && (strcmp(CC,"no-cache") == 0) )
	{
		C->cache=false;
		return("OK");
	}
	if ( (response == false) && (strcmp(CC,"no-cache") == 0) )
	{
		return("getNotFromCache");
	}
	else if (strcmp(CC,"max-age") ==0)
	{
		time_t current;
		time(&current);

		if (response)
		{
			C->cache=true;
			return("OK");
		}
		else
		{
			/* If both the new request and the cached entry include
			"max-age" directives, then the lesser of the two values
			is used for determining the freshness of the cached entry
			for that request. */
			if (maxage != 0)
			{
				if (maxage < (C->freshness - C->last_access))
					C->freshness=maxage+C->last_access;
			}

			if (current >= C->freshness)
			{
				C->cache=true;
				return("OK");
			}
			else
			{
				if (debug)
				printf("[CacheControlProcedure] Else getFromCache\n");
				return("getFromCache");
			}
		}
	}
	return("OK");
}


/************************************************************************/
// FunctionName : PragmaProcedure
//
// Description  : treat the Pragma header
//
// Parameters
// [in]
//			struct sCache *C : used to define if the cache is activated
//			char *P			 : Pragma parameter found before
//			bool response	 : true if it's a response message
// [out]
//			strutc sCache 	 : C->cache is true if we can cache the response
//
// Returns
//			char *	: "OK" means do nothing special
//					  "getNotFromCache" means we must not get from cache
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

char *PragmaProcedure(char *P, struct sCache *C, enum bool response)
{
	if ((response) && (strcmp(P,"no-cache") == 0))
	{
		C->cache=false;
		return("OK");
	}
	else if ((!response) && (strcmp(P,"no-cache") == 0))
	{
		return("getNotFromCache");
	}
}


/************************************************************************/
// FunctionName : ExpiresProcedure
//
// Description  : treat the Expires specifications
//
// Parameters
// [in]
//			struct sCache *C : used for the freshness value
//			time_t E		 : contain the expire value found before
// [out]
//			nothing
//
// Returns
//			char *	: "getFromCache" means we can get from cache
//					  "getNotFromCache" means we must not get from cache
//
// Calls
//			nothing
//
// Globals
//			none
//
/************************************************************************/

char *ExpiresProcedure(struct sCache *C, time_t E)
{
	time_t current;
	current = time(NULL);
	C->freshness = E;

	if (E > current)
		return("getFromCache");
	return("getNotFromCache");
}

