NuHTTP

Wednesday, 28 Nov 2007

Git users can now download NuHTTP from code.neontology.com. Built as a Cocoa framework, NuHTTP includes a web server and a simple HTTP client class.

NuHTTP is the web server component that I used in my screencast demo; in the demo, I first used NuAnywhere to inject a console into Delicious Library, then I used that console to load a small server based on NuHTTP.

Unlike other server components, NuHTTP is directly integrated with the Cocoa run loop. That means having no scary thread interactions to worry about; you can catch and serve HTTP requests in the main thread. And since your server is in Cocoa's run loop, you don't have to make any kludgy calls to event handlers while it is running.

The seed for NuHTTP was planted by Jurgen Schweizer, whose Mac DevCenter article titled How to Write a Cocoa Web Server described a simple way to embed an HTTP server in the Cocoa run loop.

NuHTTP contains many changes, including a generalization of the underlying server to be used as an embeddable component, the ability to handle HTTP POSTs as well as GETs, and the ability to simply and easily write a server controller in Nu to handle HTTP requests. NuHTTP also synchronizes request handling by inserting events into the main run loop using NSObject's performSelectorOnMainThread:withObject:waitUntilDone:.

Another major influence on NuHTTP is Blake Mizerany's Sinatra, a new Ruby micro-framework for elegantly declaring HTTP action handlers. NuHTTP includes get and post macros that are patterned after the get and post functions in Sinatra.

NuHTTP includes the definition of a small site that demonstrates basic handling of GETs and POSTs and the use of cookies to manage logged-in sessions. To run it, first build NuHTTP with nuke, then run the following at the command line: nuhttpd --verbose Then visit localhost:3000 on your local system.

See the nuhttpd source file for more options, and nu/sample.nu for the sample site definition. There's also complete nudoc-generated reference documentation for NuHTTP. Please try it, and then send me your questions. I think it's quite self-explanatory, but know that I'm biased. I also know that this is far from complete, so tests, patches, and bug-fixes are all welcome!

NuHTTP is released without warranty under the same license as Nu.