Weblogs: Atom

Perl AtomAPI implementation

Sunday, August 10, 2003

I've spent the last few weeks trying to get my head around REST (Representational State Transfer). Its a web services architecture much like the Web itself, and uses the same HTTP vocabulary as the Web. This means that REST-based webservices can work on normal webservers without any additional support.

Decision to use Perl

After getting my head around the concepts of REST and web services, I needed to select a language - preferably one I can already use on my websites. PHP took a while to discard as an option since it requires a specific configuration change to allow the raw post data to be accessed, and there seemed to be no easy way of getting hold of PUT and DELETE requests.

So it had to be Perl. I then spent some time figuring out whether CGI.pm contained enough to handle PUT and DELETE requests, and sadly only GET and POST are handled. I looked briefly at the documentation of SOAP::Transport::HTTP and XMLRPC::Transport::HTTP and didn't see much hope using them, so I went ahead and wrote my own.

Once the basic CGI functions are written, the webservices handling turned out to be a piece of cake. It only took me a few hours or so of effort over the last week to get an implementation done.

The client/server catch-22

The biggest problem with writing a new webservice is that its difficult to get started. You basically have to write two programs before you can do anything: a server and a client. And you can't try either of them until they are both working. So I hacked together a client (using Apache's Java HTTPClient) that posted an XML file to a URL (just a phpinfo script at first). Once that sent back the entire phpinfo output, I could then try to implement a service to actually do something with the client's XML file.

The helping hand of Ken MacLeod

I took Ken MacLeod's shell script example and supporting documentation as a guide and basically reimplemented it in Perl. The source is available.

Implementation details

The entry point for the service is at http://www.isolani.co.uk/cgi-bin/atomrest.pl/blog/ where /blog is the weblog resource itself. A GET on this URL returns a very minimal summary of entries found. This can be compared with the directory list http://www.isolani.co.uk/rest/blog/.

Posting an XML document to the URI http://www.isolani.co.uk/cgi-bin/atomrest.pl/blog/ adds a new entry - and this is listed immediately by GETting http://www.isolani.co.uk/cgi-bin/atomrest.pl/blog/ - this GET request lists the URI for the blog entry. The blog entry can then be retrieved by a GET to this URI, or updated by a PUT to this URI.

Terms and conditions of use

This implementation is in no means production ready or bug-free. The API it is based on is not formalised. There are bugs and security "features" with this code. Feel free to take the source code and play around with it - that's what I'll be doing. This is my first attempt at writing a webservice, so I've probably made some howlers, so don't treat this code as anything more than the random set of characters I extracted from /dev/null.

If you are writing a client and need a service to test it on, I'll volunteer this as long as you don't attempt to hack it or take advantage of my non-existant security. Also please don't hammer it. If I do run into a problem with resource or bandwidth issues, I'll probably have to take it down. Thanks.


[ Weblog | Categories and feeds | 2011 | 2010 | 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 ]