struct web_server {
        int socket;
        FILE *weblog;
        struct gethandler *gethandler;
        struct web_client *client;
};

this structre have server side socket (an listen socket)

Output file stream to log file

a list of gethandlers

	struct gethandler {
		char *str;
		void (*func)();
		struct gethandler *next;
	};      

a list of clients


struct web_client {
        int socket;
        struct outstream *outstream;
        struct sockaddr_in sa;
        int salen;
 
        char *rbuf;
        int rbufsize;
 
        char *obuf;
        int obufsize;
        int writedsize;
	char *HTTPdirective;
 
        unsigned char stat;  /* 0001b idle,0010b down streaming, 0011 done down streaming, 0100b out streaming,0101 done out st */
	struct web_client *next;
};                      

how it works:

first we initialise the web_server var

struct web_server server;

int PORT=80;
char *logfilepath="./webserver.log"
web_server_init(&server,PORT,logfilepath);

web_server_init do:
	initialize server->socket (create a socket,bind and listen);

	if the logfilepath is NULL so set's server->weblog to NULL, web_log function does nothing when
		server->weblog var is NULL else it fprintf the log to FILE stream

	initialize client list (allocate mem to first node (the linked list head))

	initalize gethandler list (allocate mem to first node (the linked list head))

	sets sigpipe to sig ignore

	(have error checks return 1 if sucesseful setted 0 if error ocurred)


the next procedure is to add gethandlers, gethandlers are matched w/ regular expressions (POSIX regexp)

web_add_handler(&server,".* /.*\\.html",function); handles all GET, POST, WHATEVER .html things

function should be as 'void function ()' prototype

web_add_handler do:
	allocate a node and link to server->gethandler list

void function () 

	to pass data to client just do an printf this data is passed in a stream to the client socket
// Change	
	ClientInfo->Query("handle") do
		allocate mem a return the pointer allocated by malloc with the value of handle
		
		GET /index.html?handle=1234&xpto=blah HTTP/1.0
		in this this case returns a pointer to a char * (allocated by malloc) containing "1234"

	web_client_getpostval("handle") do
		same as web_client_getquerystring but using a diferent method "POST"

	web_client_getreqname() do
		This function returns an allocated pointer to requested "file"
		
		GET /index.html?handle=1234&xpt=blah HTTP/1.0
		this case the function returns an allocated pointer containg "index.html"

	web_client_getreqline() do
		returns the entire "GET /xpto HTTP/1.0"
	
	web_client_addstream(FILE *) do
		add an stream to client outstream... when web_server_run is processing client, it process 
		this stream reading from this FILE stream writing to client socket
		
	

web_server_run(&server) 
	This is the core function
	this function checks if there is new connections and process the clients requests...
	
	first checks if there is new data (using select) in the server socket (the listen socket)

	if there is new data in server socket then allocates a new client node
		setting up the client->socket, client->sa (socket address) and client->salen (socket address length)
		(client->socket=accept(server->socket,&client->sa,&client->salen);)
		and add it to the client list..
		add_client function does initialise the rbuf,rbufsize,obuf,obufsize,writedsize,stat,etc.. from
		the client node... initialize the outstream list (adding an allocated node to the head of linked list)
	
	runs every clients 
		the read state
			checks for newdata on client socket(read_client), until it receivs ("\r\n\r\n") 'end of http headers' or the client
			close the connection, passes to next state of client (process_client)
		
		the process state 
			does a check w/ http headers and the gethandler list expression, 
			when it matches create an empty stream (mem stream or tmpfile stream) swap stdout to that stream (so that printf works on that stream, and not really stdout)
			and execute the gethandler function then reset the stdout to normal value.. and change to the next state
		
		the output state
			runs every outstreams 'freading' and writing to the client socket, since the client socket is
			non_block, i fseek the stream to writedsize (couse don't write every thing we say (non_block))
			
		after all of this or when some error ocurred (no expression match, or broken pipe) it delete's
		outstream nodes from the client (free's) and delete this client node (free)

	change to next client





	

	



	

























