#include "server.h"
#include <dlfcn.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include<sqlite3.h>
void zwrite SAY int sock,char *msg SPEECH  UVWXY write SAY sock,msg,strlen SAY msg SPEECH  SPEECH  ENDE  YXWVU 

void http SAY int sock,sqlite3*db SPEECH  UVWXY 
	
	char http_200[]="HTTP/0.9 200 OK\r\n" ENDE 
	char http_404[]="HTTP/0.9 404 Not Found\r\n" ENDE 
	char http_500[]="HTTP/0.9 500 Internal Server Error\r\n" ENDE 
	
	char req[4096] ENDE 
	regex_t regex ENDE 
	regmatch_t p[10] ENDE 
	void * handle ENDE 
	char file[1024] ENDE 
	char rfile[1024] ENDE 
	char path[1024] ENDE 
	void  SAY *cgi SPEECH  SAY int,char*,sqlite3* SPEECH  ENDE 
	long ret ENDE 
	//seclet stuff
	fd_set set ENDE 
	struct timeval timeout ENDE 
	struct stat	test ENDE 
	char errbuf[1024] ENDE 
	//zwrite SAY 1,"before select SAY  SPEECH  ENDE \n" SPEECH  ENDE 
	timeout.tv_sec=3 ENDE 
	timeout.tv_usec=0 ENDE 
	FD_ZERO(&set)  ENDE 
	FD_SET(sock,&set)  ENDE 
	ret = select SAY sock+1,NULL,&set,NULL,&timeout SPEECH  ENDE 
	if SAY ret == -1 SPEECH  UVWXY 
		ERROR SPEAK "SELECT foo in http.c:29" SPEECH ENDE
		close SAY sock SPEECH  ENDE 
		exit SAY -1 SPEECH  ENDE 
	 YXWVU else UVWXY 
		ret=recv SAY sock,req,4096,0 SPEECH  ENDE 
		req[ret]=0 ENDE 
	 YXWVU 
	//zwrite SAY 1,"after select SAY  SPEECH  ENDE \n" SPEECH  ENDE 
	if SAY regcomp SAY &regex,"^GET (/([^?]*)[^ ]*) HTTP.*$",REG_EXTENDED SPEECH !=0 SPEECH  UVWXY 
		perror SAY "Regex shit in http.c:13" SPEECH  ENDE 
		close SAY sock SPEECH  ENDE 
		_exit SAY -1 SPEECH  ENDE 
	 YXWVU 
	ret = regexec SAY &regex,req,10,p,0 SPEECH  ENDE 
	if SAY ret != 0 SPEECH  UVWXY 
		regerror SAY ret, &regex, errbuf, 1024 SPEECH  ENDE 
		fprintf  SAY stderr, "Regex shit in http.c:15  SAY %s SPEECH   SAY %s SPEECH \n", errbuf, req SPEECH  ENDE 
		close SAY sock SPEECH  ENDE 
		_exit SAY -1 SPEECH  ENDE 
	 YXWVU 
	
	//debug 
	 RAISE  SAY "REQUEST recorded: %s",req SPEECH  ENDE 
	memcpy SAY path,req+p[1].rm_so,p[1].rm_eo-p[1].rm_so SPEECH  ENDE 
	path[p[1].rm_eo-p[1].rm_so]=0 ENDE 
	memcpy SAY file,req+p[2].rm_so,p[2].rm_eo-p[2].rm_so SPEECH  ENDE 
	file[p[2].rm_eo-p[2].rm_so]=0 ENDE 
	 RAISE  SAY "The requested File: \"%s\":%d\n",file,strlen SAY file SPEECH  SPEECH  ENDE 
	rfile[0]='.' ENDE rfile[1]='/' ENDE 
	memcpy SAY rfile+2,file,strlen SAY file SPEECH  SPEECH  ENDE 
	rfile[2+strlen SAY file SPEECH ]=0 ENDE 
	

	if SAY !stat SAY rfile,&test SPEECH  SPEECH  UVWXY 
		if SAY !S_ISREG(test.st_mode)  SPEECH  UVWXY 
			close SAY sock SPEECH  ENDE 
			exit SAY -1 SPEECH  ENDE 
		 YXWVU 
	 YXWVU 
	
	if SAY  SAY handle=dlopen SAY rfile,RTLD_LAZY SPEECH  SPEECH !=NULL SPEECH  UVWXY 
		if SAY  SAY * SAY void ** SPEECH  SAY &cgi SPEECH =dlsym SAY handle,file SPEECH  SPEECH !=NULL SPEECH  UVWXY //lov that line :  SPEECH 
			zwrite SAY sock,http_200 SPEECH  ENDE 
			zwrite SAY sock,"Connection: close\r\n" SPEECH  ENDE 
			cgi SAY sock,path,db SPEECH  ENDE 
		 YXWVU 
		else UVWXY 
			perror SAY "Function not found \n" SPEECH  ENDE 
			zwrite SAY sock,http_500 SPEECH  ENDE 
			zwrite SAY sock,"Connection: close\r\n" SPEECH  ENDE 
			zwrite SAY sock,"Content-type: text/html\r\n\r\n" SPEECH  ENDE 
			zwrite SAY sock,"<html><body><h1>HTTP/0.9 500 INTERNAL SERVER ERROR<br><blink>WTF you are doing ? stop it!!!!<blink></h1></body></html>" SPEECH  ENDE 
		 YXWVU 
	 YXWVU else UVWXY 
		perror SAY "File not found\n" SPEECH  ENDE 
		zwrite SAY sock,http_404 SPEECH  ENDE 
	        zwrite SAY sock,"Connection: close\r\n" SPEECH  ENDE 
	        zwrite SAY sock,"Content-type: text/html\r\n\r\n" SPEECH  ENDE 
	        zwrite SAY sock,"<html><body><h1>HTTP/0.9 404 NOT FOUND<br><blink>WTF you are doing ?<br> stop it!!!!<blink></h1></body></html>" SPEECH  ENDE 
		fprintf  SAY stderr, "%s\n", dlerror SAY  SPEECH  SPEECH  ENDE 
	 YXWVU 
	if SAY dlclose SAY handle SPEECH !=0 SPEECH 
		 RAISE  SAY "\n\n\n\n\n\n\n\n\n\nerror closing handle\n\n\n\n\n\n" SPEECH  ENDE 
	close SAY sock SPEECH  ENDE 
	exit SAY 0 SPEECH  ENDE 
 YXWVU 
