I am trying to write my own http 1.1 server, just for fun, and learn more about HTTP, sockets, and streams.
I got a good start, I think, only by delivering static pages (using c, which I would rather stay for a while). I have a test page that I wrote some time ago and deliver it ~ 50 files in 124 ms according to chrome, without using streams or supported sockets.
It was very difficult for me to work with the flow / continuation of work. There are practically no resources on the Internet (which I can find in my hours of work on Google) that explain in detail the connections to a live connection. If anyone could recommend a good book on programming on an HTTP server, I would really appreciate it.
I have already done some streaming and socket programs, creating a simple chat program, so I have at least some experience working with it.
The problem I am facing is that when I try to enable streams, the client browser makes several connections. Somewhere along the line, the server gets confused, and the client just sits there, waiting for answers, and the server stops doing anything. I send the Connection: Keep-Alive header, but that doesnโt change anything, and when I turn on keep-alive and create a loop to receive requests in the stream function, it stops until the connection is closed.
I would appreciate it if someone could give me some pseudo-code on how to maintain work / threads, working for this, so that the client stops creating multiple connections at a time.
A brief description of what is happening:
main function
load in static pages to large array of fileinfo struct that hold the file data and length create the socket set it to listen to port 80 set it to listen for 10 connections at a time(i know this is low...) start an endless loop block while waiting for someone to connect check if it a localhost connection shutdown the server otherwise start a thread(with pthread), sending it the socket variable loop
code>
Thread Function
setsock opt for 3 sec timeout on send/recv and enable Keep-alive start endless loop read in request if request timed out, break the loop Validate Request function call Create Reponse function call Send response if request contained Connection: close header break the loop loop close socket return
code>