开发者

HTTP Server Programming

开发者 https://www.devze.com 2023-01-21 14:23 出处:网络
I\'m att开发者_如何学编程empting to write my own http 1.1 server, just for fun and learning about more about HTTP, sockets, and threading.

I'm att开发者_如何学编程empting 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


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.)


I'd recommend grabbing the source for Apache and seeing how they handle it. There's not much point in psuedo code when you can see how the real thing works.


Perhaps you could look at Apache's code for some clues. It is written in C.

Hopefully someone will come along and give a more detailed answer :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号