Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Server Programming

I'm attempting to write my own http 1.1 server, just for fun and learning about more about HTTP, sockets, and threading.

I've gotten a good start i think with only delivering static pages (using c, which I would prefer to stay in for the time being). I have a test page I wrote a while ago and deliver it's ~50 files in 124ms according to chrome, without using threads or keep-alive sockets.

I've found it very difficult to get threading/keep-alive working at all. There are little to no resources on the web (that I can find in my hours of Googling) that explain keep-alive connections in detail. If anyone could recommend a good book on HTTP server programming, I would greatly appreciate it.

I've done some threading and socket programming before by making a simple chat program, so I have at least some experience with it.

The issue I'm having is that when I attempt to incorporate threads, the client browser sets up multiple connections. Somewhere along the line, the server gets confused and the client just sits there waiting for responses and the server stops doing anything. I send the Connection: Keep-Alive header, but that doesn't change anything and when I incorporate keep-alive it and create a loop for getting requests in the threaded function, it stalls until the connection is closed.

I would appreciate it if someone could give me some pseudo code on how to get keep alive/threading working for this so the client stops creating multiple connections at a time.

A brief description of whats going on:

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's a localhost connection
          shutdown the server
      otherwise
           start a thread(with pthread), sending it the socket variable
 loop


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

like image 380
Wolftousen Avatar asked Oct 25 '10 01:10

Wolftousen


People also ask

What is HTTP server software?

An HTTP server is software that understands URLs (web addresses) and HTTP (the protocol your browser uses to view webpages). An HTTP server can be accessed through the domain names of the websites it stores, and it delivers the content of these hosted websites to the end user's device.

What is HTTP server in Python?

HTTP Web Server is simply a process which runs on a machine and listens for incoming HTTP Requests by a specific IP and Port number, and then sends back a response for the request. Python has a built-in webserver provided by its standard library, can be called for simple client-server communication.


1 Answers

I would recommend looking at GNU libmicrohttpd. It focuses squarely on providing a framework upon which to build HTTP 1.1 servers. It is small and supports keep-alive with and without threading. (Personally I use it without threading. It has several threading models too.)

Even if you decide to write your web server from scratch, I would suggest looking at libmicrohttpd to gain insight in not only how the protocol works, but how the library models "the work flow" of a web server in a very clean way. I think it is a mistake to imagine that keep-alive implies threading and I think it is an impediment to understanding keep-alive.

(Regarding Apaches' credits as a web server, it is pretty huge, and there is a lot in there not related to protocols, but rather things like its plugin system and so on.)

like image 100
Prof. Falken Avatar answered Oct 13 '22 00:10

Prof. Falken