[Catalyst] A plugin for handling If-Modified-Since?

Robert Rothenberg robrwo at gmail.com
Wed Dec 5 15:48:57 GMT 2012


I'm working on a RESTful API for an application (much thanks for
Catalyst::Controller::REST, by the way) and need to handle the
If-Modified-Since header to efficiently check when things have changed.

It seems my controllers will follow the pattern:

  1. Initial validation of the request and get the base object that
     the request is about.

  2. Check for If-Modified-Since header.

     If it is present and no older than the last-modified date of
     the object, return a 304 status with no body.

  3. Otherwise, set the Last-modified and Expires headers. (The latter
     to keep Firefox from caching the result too long.)

  4. If a HEAD request, exit.

     Otherwise return the object, which involves some more reads from the
     database etc.

I've looked at the Cache::HTTP plugin, but it doesn't do what I want, which
is to pre-empt running queries if they don't need to be run.

Is there another plugin for doing this sort of thing?

If not, I am thinking of writing a plugin that would work something like

  $c->if_unmodified( headers => $sub1, content => $sub2 );

where $sub1 would be code to set the last-modified and maybe weak etags
(based on the last-modified if nothing else is specified).

The method would check for a If-Match and If-Modified-Since headers, etc. If
they matched, it would return a 304 error, otherwise it would run $sub2.

Does this seem a worthwhile endeavor?




More information about the Catalyst mailing list