[Catalyst] HTTP headers returned from GET vs HEAD

Trevor Leffler tleffler at uw.edu
Wed Apr 23 23:44:03 GMT 2014


I've been playing with a toy application and inspecting its responses to 
various HTTP methods and noticed that Content-Length is missing from the 
HEAD response.  Both Catalyst::Action::RenderView and C::A::Serialize 
(from the C::A::REST package) seem to take the easy way out and "return 
1 if $c->req->method eq 'HEAD'".  Does anyone with CAR history know why 
this is?

To provide message-body-based headers (content-length, last-modified, 
content-md5, etc) I'm doing this:

sub render : ActionClass('RenderView') { }

sub end : Private {
   my $self = shift;
   my ($c) = @_;

   $c->forward('render');

   # see also Catalyst::Action::RenderView or ::Serialize
   if ($c->req->method eq 'HEAD') {
     my $view = $c->view
       || die sprintf("%s could not find a view to forward to.\n", 
__PACKAGE__);
     $c->forward($view);
   }

   # fun with headers, for example...
   # or enable P::M::ContentMD5
   if ($c->res->has_body) {
     my $md5 = Digest::MD5::md5_hex($c->res->body);
     $c->res->headers->header(Content-MD5 => $md5);
   }
}

I've noted that while $c->res->body now has my rendered view, it gets 
removed by Plack::Middleware::Head prior to returning the response, so I 
don't have to worry about that detail.

--Trevor



More information about the Catalyst mailing list