Tuesday, August 20, 2024

Fixed up an old web server I was writing 12 years ago.

It used epoll, sendfile, and threads. I stopped working on it because I didn't know how to make mutexs work for the threads.  But the big AI models know how to do that and fixed me right up. Once I got that working I uploaded it to github as the Simple Advanced Web Sever.

The webserver.c program looks like this:



But I did not stop there.  My next trick was to add python scripting to the c program so it can load and run python scripts.  And today I hooked the url parameters into the inputs of the python script.  



I am exploring a way to allow the threads to just queue up what needs to be done and move onto the next request in order to speed up the processing.  This is needed in order to properly handle ssl in the application anyway.   

The server will create an array of pointer to request queues, one for each request thread.  There will always be one node on the list, the threads will create nodes and add it to the end of the list, keeping a a pointer to the current end of their request queue.  This prevents any thread from accessing the same data at the same time.  The last node will always be left on the list, just a done flag set to true. 

These nodes will be pulled off the end of the queue and placed into a work list, if a node waits more than a few milliseconds it will be copied, the copy put onto the work list, and the done flag set so we know we processed it.  Once that node has a next element then we can advance the array pointer to the next node and mark free the current node. 

To process the working list in the server.c file I want to set everything to non blocking and create different lists if they are ssl or fd sendfile or just fd. 

The data structure identifying the connection will be a netstream object. This object will have things added by each layer.


struct net_stream {
    int fd;
    SSL *ssl;
    int is_ssl;
    thread * context;

    int done;
    char* filename;
    char* data;
    net_stream * list;
}


This handles the connection and the processing to be done depending on what is set in the object. It contains enough memory to function for different tasks depending on what you want to do. 

At that point the request threads become very fast if all they ever do is call a function with process the header request and continue on. 

No comments:

Post a Comment