From: Andy Grundman Date: Wed, 7 Dec 2005 18:11:57 +0000 (+0000) Subject: Automatically determine Content-Length when serving a filehandle X-Git-Tag: 5.7099_04~788 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=8f62c91addd1c43b8639b2a11b0eb007d9834f46 Automatically determine Content-Length when serving a filehandle --- diff --git a/Changes b/Changes index d00a42f..b7ec4c3 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ This file documents the revision history for Perl extension Catalyst. 5.62 + - Automatically determine Content-Length when serving a + filehandle. - Exceptions now return status 500 - Updated for Module::Install 0.40 - Fixed additional file installation for multi level app names diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 7b56a60..dc56b88 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -10,6 +10,7 @@ use Catalyst::Request; use Catalyst::Request::Upload; use Catalyst::Response; use Catalyst::Utils; +use File::stat; use NEXT; use Text::SimpleTable; use Path::Class; @@ -1015,7 +1016,20 @@ sub finalize_headers { # Content-Length if ( $c->response->body && !$c->response->content_length ) { - $c->response->content_length( bytes::length( $c->response->body ) ); + # get the length from a filehandle + if ( ref $c->response->body && $c->response->body->can('read') ) { + if ( my $stat = stat $c->response->body ) { + $c->response->content_length( $stat->size ); + } + else { + $c->log->warn( + 'Serving filehandle without a content-length' ); + } + } + else { + $c->response->content_length( + bytes::length( $c->response->body ) ); + } } # Errors