Static::Simple, documented internal methods for coverage, fixed binmode call
[catagits/Catalyst-Plugin-Static-Simple.git] / lib / Catalyst / Plugin / Static / Simple.pm
index de6c229..708b265 100644 (file)
@@ -1,23 +1,19 @@
 package Catalyst::Plugin::Static::Simple;
 
 use strict;
+use warnings;
 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 use File::stat;
+use IO::File;
 use MIME::Types;
 use NEXT;
 
-if ( Catalyst->VERSION le '5.33' ) {
-    require File::Slurp;
-}
-
-our $VERSION = '0.10';
+our $VERSION = '0.11';
 
 __PACKAGE__->mk_classdata( qw/_static_mime_types/ );
 __PACKAGE__->mk_accessors( qw/_static_file
                               _static_debug_message/ );
 
-# prepare_action is used to first check if the request path is a static file.
-# If so, we skip all other prepare_action steps to improve performance.
 sub prepare_action {
     my $c = shift;
     my $path = $c->req->path;
@@ -30,7 +26,7 @@ sub prepare_action {
         }
         if ( $path =~ $re ) {
             if ( $c->_locate_static_file ) {
-                $c->_debug_msg( "from static directory" )
+                $c->_debug_msg( 'from static directory' )
                     if ( $c->config->{static}->{debug} );
                 return;
             } else {
@@ -51,7 +47,6 @@ sub prepare_action {
     return $c->NEXT::ACTUAL::prepare_action(@_);
 }
 
-# dispatch takes the file found during prepare_action and serves it
 sub dispatch {
     my $c = shift;
     
@@ -68,14 +63,12 @@ sub dispatch {
     }
 }
 
-# finalize serves up final header information
 sub finalize {
     my $c = shift;
     
     # display all log messages
     if ( $c->config->{static}->{debug} && scalar @{$c->_debug_msg} ) {
-       $c->log->debug( "Static::Simple: " .
-           join( " ", @{$c->_debug_msg} ) );
+        $c->log->debug( 'Static::Simple: ' . join q{ }, @{$c->_debug_msg} );
     }
     
     if ( $c->res->status =~ /^(1\d\d|[23]04)$/xms ) {
@@ -91,6 +84,10 @@ sub setup {
     
     $c->NEXT::setup(@_);
     
+    if ( Catalyst->VERSION le '5.33' ) {
+        require File::Slurp;
+    }
+    
     $c->config->{static}->{dirs} ||= [];
     $c->config->{static}->{include_path} ||= [ $c->config->{root} ];
     $c->config->{static}->{mime_types} ||= {};
@@ -99,7 +96,7 @@ sub setup {
     $c->config->{static}->{debug} ||= $c->debug;
     if ( ! defined $c->config->{static}->{no_logs} ) {
         $c->config->{static}->{no_logs} = 1;
-    }
+    }    
     
     # load up a MIME::Types object, only loading types with
     # at least 1 file extension
@@ -127,9 +124,9 @@ sub _locate_static_file {
         if ( ref $dir eq 'CODE' ) {
             eval { $dpaths = &$dir( $c ) };
             if ($@) {
-                $c->log->error( "Static::Simple: include_path error: " . $@ );
+                $c->log->error( 'Static::Simple: include_path error: ' . $@ );
             } else {
-                unshift( @ipaths, @$dpaths );
+                unshift @ipaths, @$dpaths;
                 next DIR_CHECK;
             }
         } else {
@@ -173,7 +170,7 @@ sub _serve_static {
     my $type = $c->_ext_to_type;
     
     my $full_path = $c->_static_file;
-    my $stat = stat( $full_path );
+    my $stat = stat $full_path;
 
     # the below code all from C::P::Static
     if ( $c->req->headers->if_modified_since ) {
@@ -191,17 +188,19 @@ sub _serve_static {
     if ( Catalyst->VERSION le '5.33' ) {
         # old File::Slurp method
         my $content = File::Slurp::read_file( $full_path );
-        $c->res->output( $content );
+        $c->res->body( $content );
     }
     else {
-        # new write method
-        open my $fh, '<', $full_path 
-            or Catalyst::Exception->throw( 
+        # new method, pass an IO::File object to body
+        my $fh = IO::File->new( $full_path, 'r' );
+        if ( defined $fh ) {
+            binmode $fh;
+            $c->res->body( $fh );
+        }
+        else {
+            Catalyst::Exception->throw( 
                 message => "Unable to open $full_path for reading" );
-        while ( $fh->read( my $buffer, 4096 ) ) {
-            $c->res->write( $buffer );
         }
-        close $fh;
     }
     
     return 1;
@@ -220,7 +219,7 @@ sub _ext_to_type {
         if ( $type ) {
             $c->_debug_msg( "as $type" )
                 if ( $c->config->{static}->{debug} );            
-            return $type;
+            return ( ref $type ) ? $type->type : $type;
         }
         else {
             $c->_debug_msg( "as text/plain (unknown extension $ext)" )
@@ -412,6 +411,28 @@ directory.  This approach is recommended for production installations.
         SetHandler default-handler
     </Location>
 
+=head1 INTERNAL EXTENDED METHODS
+
+Static::Simple extends the following steps in the Catalyst process.
+
+=head2 prepare_action 
+
+prepare_action is used to first check if the request path is a static file.
+If so, we skip all other prepare_action steps to improve performance.
+
+=head2 dispatch
+
+dispatch takes the file found during prepare_action and writes it to the
+output.
+
+=head2 finalize
+
+finalize serves up final header information and displays any log messages.
+
+=head2 setup
+
+setup initializes all default values.
+
 =head1 SEE ALSO
 
 L<Catalyst>, L<Catalyst::Plugin::Static>,