Sunday, October 25, 2009

Web Server in Haskell

I have set up a goal for myself to write a relatively simple web server in Haskell. I will gradually make the server more complicated to support more features.  Here is a list of features I'm planning to support in the final version (in some random order):
  • Web Service support
  • Logging and Debugging
  • Templates
  • CSS
  • Javascript/Ajax calls
  • Cookies
  • Login
  • Diversity in Browsers
  • Database
  • HTTP client calls to other servers
  • XML parsing and generation
  • TCP-level calls to backends (with possibly RPC like abstractions)
  • Redirection
  • I18N and Localization
  • Security
Serving URLs

I have found two ways of setting up web services.
  • Writing the service as a CGI application and run in a general-purpose server such as Apache.  The tutorial "Practical Web Programming in Haskell" is a good introduction on how to write a cgi application. 
    • Pros: quick and easy to write.
    • Cons: Too heavy weight if your server is specialized and needs only limited resources.
  • Writing a standalone server using Happstack framework (I had mentioned Happs, the predecessor of Happstack in my earlier version of the posting. Thanks to Antoine, I realized there is a more frequently maintained version...)
    • Pros: The framework supports lots of things: session management, concurrency,....
Template Support
The library HStringTemplate is probably the best shot. This Haskell port of the Java StringTemplate library designed by Terrence Parr has a well-thought design. Terrence has done a nice job of thinking over and addressing many issues from which many other templating systems suffer. Unfortunately, and as the name suggests the templates are just strings with holes. I would have liked to see a system where the templates are XHTML documents with holes. This might require some language design and may look like Links (but for use in arbitrary languages).  The advantage would be that the templates may now be type checked and statically verified before rendering/filling holes. And if the holes are typed too, it would be possible to guarantee that the filled template would satisfy xhtml syntactic and semantic requirements at compile time.

HTTP Request Support
The HTTP package from HackageDB should be sufficient for this purpose.

Low Level Network Support
To talk to backends that only talk TCP, the package Network should have sufficient support. The extensions found in the package HTTP should also be helpful.

XML Parsing and Generation
The excellent library Haskell XML Toolbox is perfect for this. It allows conversion of XML data into and from Haskell datatypes. It seems to have everything I would need.


  1. Happstack is the successor project to HAppS, and does reasonably frequent releases:

    I haven't worked on earlier versions, so I'm not sure which parts rely on XSLT - I've done plenty without encountering it at all.

  2. Thanks for the tip! :-) I didn't expect to get comments so quickly!

    I am still revising my posting. I will fix my comments and refer to happstack.