views:

144

answers:

3

I want to make the most lightweight possible HTTP server in C that supports PHP and possibly FastCGI if it will make a huge difference.

I'm not sure how to implement PHP support. Does it just call PHP.exe with the path to a .php file and read the output? What about things like header () in PHP? How are those handled by the server?

And another question, is it ideal to use separate threads for each request? I don't expect a very heavy load, but I'm not 100% sure on the design aspect of this...

I'm still pretty new to C and C++ and this is a learning experience.

+8  A: 

Firstly let me say that if the goal is a lightweight HTTP server that serves PHP pages, this has already been done. Have a look at nginx.

As for a learning experience, you've chosen something that's actually fairly tough.

Multithreaded is hard at the best of times. On C/C++ (anything with manual memory allocation really) it's an order of magnitude harder.

Added to this is network communication. There are quirks to deal with, different versions of HTTP (mostly a non-issue now), all sorts of HTTP headers to deal with and so on.

The most intuitive solution to this problem is to have a process that listens to a port. When it receives a request, it spawns a process, which may exec to a PHP process if required.

This however does not scale. The first (obvious) optimization is to use threads instead of processes and some form of interthread communication. While this helps, it will still only scale so far.

Go beyond that and you're looking at async socket handling, which is fairly low level.

All of these however are fairly big projects.

Is there any particular reason you're doing this in C/C++? Or any particular reason you're learning one or both of those languages? These languages certainly have their place but they're increasingly becoming niche languages. Managed (garbage collected) languages/platforms have almost completely taken over. Joel argues that garbage collection is about the only huge productivity increase in programming in about the last 20 years and I tend to agree.

cletus
I've done this same thing (without PHP support) in Visual Basic 6 years ago, but all of the Async socket stuff was taken care of already with Microsoft's Winsock control. Also, it was Visual Basic, so it's going to be a lot harder to write a good one in C or C++. (I'd really like to stick to C and get really good at it before going to C++).Are async sockets really that complex using just APIs? I honestly have no idea because I come from Visual Basic. :P**Edit:** Thanks for the link to nginx, never knew it existed!(I still want to try to write one myself as a learning experience).
guitar-
I'm not so sure about completely taken over. Apache, Lighttpd, Nginx, Cherokee ... all written in C. There are also suitable garbage collecting malloc() replacements for C. I agree that its mostly only systems programmers that use C today, but I think the niche is a bit bigger than you describe.
Tim Post
@guitar C and C++ are really different languages with different programming paradigms. Skill with one does not imply nor necessarily help with the other.
cletus
+1  A: 

For a learning experience regarding HTTP code written in C you may also take a look at:

http://hping.org/wbox/

chad
A: 

To make your own HTTP server, I reccomend to get inspiration from other peoples code. The programmer ry famous for the node.js framework has written simple elegant code regarding this matter.

Check out his libebb library, it has a parser generated with Raegel using the easy yet powerful PEG (it's based on Zed Shaw's mongrel parser). Also check the example usage. It is really clean and usable code.

libebb is a lightweight HTTP server library for C. 
It lays the foundation for writing a web server 
by providing the socket juggling and request parsing. 
By implementing the HTTP/1.1 grammar provided 
in RFC2612, libebb understands most most valid HTTP/1.1 
connections (persistent, pipelined, and 
chunked requests included) and rejects invalid or 
malicious requests. libebb supports SSL over HTTP. 

Regarding PHP-Server coupling, the easiest way is CGI but if you feel adventurous dig into php source code under SAPI (Server API) modules to see how to do it.

clyfe