HTTP Server Programming

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>

+6
c multithreading sockets
source share
3 answers

I would recommend looking at GNU libmicrohttpd . It focuses on providing the infrastructure for building HTTP 1.1 servers. It is small and supports continuous operation without threading. (Personally, I use it without streaming. It also has multiple streams.)

Even if you decide to write your web server from scratch, I would suggest looking at libmicrohttpd to get an idea of โ€‹โ€‹not only how the protocol is, but how the library models the "workflow" of the web server in a very clean way. I think it is a mistake to assume that keep-alive implies threads, and I think this is an obstacle to understanding keep-alive.

(As for Apaches credits as a web server, it is quite large, and there are a lot of things that are not related to protocols, but rather things like its plug-in system, etc.)

+2
source

I would recommend grabbing the source of Apache and see how they handle this. There is not much point in psuedo code when you can see how the real thing works.

+1
source

Perhaps you can see the Apache code for some hints. It is written in C.

I hope someone comes and gives a more detailed answer :)

+1
source

All Articles