libwebserver Engine
How it works?
Libwebserver it's separated into pieces as the module that controls the server, client, clientinfo, handlers,memory, connections, logs etc..
The first function called (w/o openssl) would be web_server_init() this function initialize the web_server mem structure, opening the
server socket (listen at port), the log file, initalize gethandler and client lists and stats...
once initialized them, Next Procedure is web_server_addhandler() this function installs
the handlers related to C functions
Next step is just put web_server_run() in a running loop (forked or not forked whatever), this functions
checks for connections,
if there is a connection, add a client;
reading clients
check if clients have something to read
if yes then reads at least 200000 b of data (non blocking)
if not(5 times) then marks client to all readed and start to process
processing client
grab's the request from client (GET /this_information?querystring=teste HTTP/1.0)
go check if the request is on gethandler list;
if not then write to client (404 not found);
if yes then setup the stdout (to grab the output),setup ClientInfo structure,run the function rellated to the gethandler, reset stdout
outputing client
output hole outstream from the current client (by order)(1500 bytes each time)
delete client
and so ever loop
Modules description
server.[ch] - Main server handler, contains the server structure
Dependencies - socket, weblog, client, gethandler, memory
functions:
web_server_useSSLcert() - explained in web_server_useSSLcert()
web_server_init() - calls weblog,socket, client, gethandler, memory(to initializate server list's) explained in web_server_init()
web_server_addhandler() - calls gethandler functions (to manipulate gethandlers lists from the server explained in web_server_addhandler()
web_server_run() - controls client struct flags, used client ,memory and weblog modules explained in web_server_run()
client.[ch] - functions to handle each client for the running server
Dependencies - socket, memory, weblog, gethandlers, fnmatch, outstream, clientinfo
init_client_list() -
add_client() -
delete_next_client -
read_client -
process_client -
web_client_writef() -
web_client_addstream() -
web_client_addfile() -
web_client_HTTPdirective -
Hdrdate() -
web_client_getreqline() -
TO DO
Internal data structures
struct web_server {
int socket;
FILE *weblog;
int flags;
struct gethandler *gethandler;
struct web_client *client;
int usessl;
char *cert_file
SSL_CTX *ctx;
struct {
unsigned long long connection;
unsigned long long bsent;
unsigned long long brecv;
}stat;
};
This is the server handler (is kind of the main structure) olds socket, the log file handler, gethandlers, client's stats, server flags
struct gethandler {
char *str;
void (*func)();
int flag; // WS_LOCAL
struct gethandler *next;
};
This is the gethandler structure, holds information about the called 'gethandlers'
struct web_client {
int socket;
struct outstream *outstream;
struct sockaddr_in sa;
unsigned int salen;
char *rbuf;
int rbufsize;
int newdata_try;
SSL *ssl;
X509* cert;
int writedsize;
char *HTTPdirective;
unsigned char stat;/* 0001b idle,0010b down streaming, 0011 done down streaming, 0100b out streaming,0101 done out streaming */
struct web_client *next;
};
This is the web_client structure, holds information about client socket, outstream list, socket addr, rbuf (data readed) etc.etc.