/* HTTP server */

#include "stdint.h"
#include "http.h"
#include "tcp.h"
#include "My_usart.h"

// Globalni promenne

//#define DEBUG

/* ----------------------------------------------
 Funkce vrati metodu pouzitou v REQUESTu
 --------------------------------------------- */
uint8_t HTTP_GetRequestMetod(uint8_t *RX_Buffer)
{
	// Zjisteni o jakou metodu se jedna (GET, POST, ...)
	if((RX_Buffer[OFFSET_TCP_Data+0]=='G') &
	   (RX_Buffer[OFFSET_TCP_Data+1]=='E') &
	   (RX_Buffer[OFFSET_TCP_Data+2]=='T') &
	   (RX_Buffer[OFFSET_TCP_Data+3]==' '))								// Overeni jestli se nejedna o metodu GET
	{
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("HTTP Req.Metod: GET",1);
			USART1_FreeMUTEX();
		#endif
		return(HTTP_REQ_METOD_GET);
	}
	else if((RX_Buffer[OFFSET_TCP_Data+0]=='P') &
	   		(RX_Buffer[OFFSET_TCP_Data+1]=='O') &
			(RX_Buffer[OFFSET_TCP_Data+2]=='S') &
			(RX_Buffer[OFFSET_TCP_Data+3]=='T') &
	   		(RX_Buffer[OFFSET_TCP_Data+4]==' '))								// Overeni jestli se nejedna o metodu GET
	{
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("HTTP Req.Metod: POST",1);
			USART1_FreeMUTEX();
		#endif
		return(HTTP_REQ_METOD_POST);
	}
	else																// Neznama metoda
	{
		return(0);
	}	
}

/* ----------------------------------------------
 Funkce vraci ukazatel na pole ve kterem je ulozen 
 pozadovany soubor pozadovaneho souboru metodou GET
 --------------------------------------------- */
uint8_t HTTP_GetRequestFromGET(uint8_t *RX_Buffer, uint16_t *ZacatekRequestu)
{
	uint16_t zacatek=0;
	uint16_t delka=0;
	uint16_t i;
	
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("HTTP GET",1);
		USART1_FreeMUTEX();
	#endif	

	while(RX_Buffer[OFFSET_TCP_Data+zacatek]!='/')						// Vyhledani zacatku '\' cesty k pozadovanemu souboru
	{
		zacatek++;
		if(RX_Buffer[OFFSET_TCP_Data+zacatek]=='\r')					// Overeni jestli se uz nedoslo na konec radku
		{
			*ZacatekRequestu = OFFSET_TCP_Data;
			return(0);													// V requestu nebyl nalezen pozadovany soubor
		}											   	
	}
	while(RX_Buffer[OFFSET_TCP_Data+zacatek+delka]!=' ')				// Vyhledani konce cesty k pozadovanemu souboru
	{
		delka++;
		if(RX_Buffer[OFFSET_TCP_Data+zacatek]=='\r')					// Overeni jestli se uz nedoslo na konec radku
		{
			*ZacatekRequestu = OFFSET_TCP_Data;
			return(0);													// V requestu nebyla nalezena oddelovaci mezera mezi pozadovanym souborem a verzi HTTP requestu
		}
	}
	if(delka>255)														// Text v requestu HTTP se nevejde do lokalniho bufferu
	{
		*ZacatekRequestu = OFFSET_TCP_Data;
		return(0);
	}
	
	*ZacatekRequestu = OFFSET_TCP_Data+zacatek;
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("> HTTP GET delka: ",0);
		USART1_SendInteger(delka);
		USART1_Send_Enter();
		USART1_Send("> HTTP GET '",0);
		USART1_FreeMUTEX();
		for(i=0;i<delka;i++)												// Zkopirovani request retezce do bufferu
		{
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Write(RX_Buffer[OFFSET_TCP_Data+zacatek+i]);
			USART1_FreeMUTEX();
		}
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("'",1);
		USART1_FreeMUTEX();
	#endif
	return(delka);
}

/* ----------------------------------------------
 Funkce vytvori Hlavicku HTTP protokolu a vrati
 jeji delku
 --------------------------------------------- */
uint16_t HTTP_SetHeader(uint8_t HTTP_Status, uint8_t *TX_Buffer, uint8_t *RX_buffer)
{
	uint16_t i=0;
	uint16_t Delka=0;
	
	// Vlozeni 1. casti HTTP hlavicky - stavovy kod HTTP
	while(HTTP_HeaderStatusCode[HTTP_Status][i])
	{
		TCP_AddDataChar(HTTP_HeaderStatusCode[HTTP_Status][i++], RX_buffer, TX_Buffer, 0);
	}
	Delka = i-1;
	// Vlozeni 2. casti HTTP hlavicky - info o serveru
	i=0;
	while(HTTP_ServerHeader1[i])
	{
		TCP_AddDataChar(HTTP_ServerHeader1[i++], RX_buffer, TX_Buffer, 0);
	}
	Delka += i-1;
	// Vlozeni 3. casti HTTP hlavicky - konec
	
	i=0;
	while(HTTP_ServerHeaderContent[i])
	{
		TCP_AddDataChar(HTTP_ServerHeaderContent[i++], RX_buffer, TX_Buffer, 0);
	}
	Delka += i-1;

	return(Delka);
}

/* Konec souboru */
