A software engineer bent on world bovine domination.
I write code at 99designs + Swiftly and on GitHub.

By Dennis Hotson.

Jan 15
Permalink

Introducing Kelpie

So call me crazy.. but I thought it would be a good idea to write a little web server in PHP.

I’d like to think that it’s fast, lightweight and hard working. :-)

Ok, show me the code

  1 class HelloWorld
  2 {
  3   public function call($env)
  4   {
  5     return array(
  6       200,
  7       array("Content-Type" => "text/plain"),
  8       array("hello world")
  9     );
 10   }
 11 }

This is what a simple Kelpie web app looks like.

The call method accepts an array of CGI like environment variables and returns an array of 3 items:

  • Status code
  • Headers as array(key => value)
  • Body text as an array (or Iterator) of strings

To actually run a Kelpie app, you need to start up a server:

  1 $server = new Kelpie_Server('0.0.0.0', 8000);
  2 $server->start(new HelloWorld());

There’s not really much more to it at the moment, I’m hoping to build a more fully featured framework on top of this.

For now I’m just having fun dabbling in writing server apps. :-D

Credits

I can’t really claim much credit I’m afraid, I haven’t really done anything original here.

Most of the code is based on the Ruby Thin web server. I also copied the Rack web server interface as you may have noticed. ;-)

I started out this project by writing a PHP extension for the Mongrel http parser.
The httpparser PHP extension code is based on this tutorial and I also used the Python bindings as a guide. It could really do with some code review, my C is a bit rusty..

Edit: Big thank you to Alexey for helping out with some review and cleanups, you should check out his PHP app server.

Download

The code is @ github. Feedback is welcome.



OMG, puppies!!1!