Static::Simple 0.13 copy to core
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Plugin / Static / Simple.pm
index d432f5f..fedd309 100644 (file)
@@ -4,21 +4,17 @@ use strict;
 use warnings;
 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 use File::stat;
+use File::Spec::Functions qw/catdir no_upwards splitdir/;
+use IO::File;
 use MIME::Types;
 use NEXT;
 
-if ( Catalyst->VERSION le '5.33' ) {
-    require File::Slurp;
-}
-
-our $VERSION = '0.10';
+our $VERSION = '0.13';
 
 __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;
@@ -33,12 +29,10 @@ sub prepare_action {
             if ( $c->_locate_static_file ) {
                 $c->_debug_msg( 'from static directory' )
                     if ( $c->config->{static}->{debug} );
-                return;
             } else {
                 $c->_debug_msg( "404: file not found: $path" )
                     if ( $c->config->{static}->{debug} );
                 $c->res->status( 404 );
-                return;
             }
         }
     }
@@ -46,13 +40,12 @@ sub prepare_action {
     # Does the path have an extension?
     if ( $path =~ /.*\.(\S{1,})$/xms ) {
         # and does it exist?
-        return if ( $c->_locate_static_file );
+        $c->_locate_static_file;
     }
     
     return $c->NEXT::ACTUAL::prepare_action(@_);
 }
 
-# dispatch takes the file found during prepare_action and serves it
 sub dispatch {
     my $c = shift;
     
@@ -69,7 +62,6 @@ sub dispatch {
     }
 }
 
-# finalize serves up final header information
 sub finalize {
     my $c = shift;
     
@@ -91,15 +83,20 @@ 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} ||= {};
-    $c->config->{static}->{ignore_extensions} ||= [ qw/tt tt2 html xhtml/ ];
+    $c->config->{static}->{ignore_extensions} 
+        ||= [ qw/tmpl tt tt2 html xhtml/ ];
     $c->config->{static}->{ignore_dirs} ||= [];
     $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
@@ -114,7 +111,7 @@ sub setup {
 sub _locate_static_file {
     my $c = shift;
     
-    my $path = $c->req->path;
+    my $path = catdir( no_upwards( splitdir( $c->req->path ) ) );
     
     my @ipaths = @{ $c->config->{static}->{include_path} };
     my $dpaths;
@@ -133,13 +130,13 @@ sub _locate_static_file {
                 next DIR_CHECK;
             }
         } else {
-            $dir =~ s/\/$//xms;
+            $dir =~ s/(\/|\\)$//xms;
             if ( -d $dir && -f $dir . '/' . $path ) {
                 
                 # do we need to ignore the file?
                 for my $ignore ( @{ $c->config->{static}->{ignore_dirs} } ) {
-                    $ignore =~ s{/$}{};
-                    if ( $path =~ /^$ignore\// ) {
+                    $ignore =~ s{(/|\\)$}{};
+                    if ( $path =~ /^$ignore(\/|\\)/ ) {
                         $c->_debug_msg( "Ignoring directory `$ignore`" )
                             if ( $c->config->{static}->{debug} );
                         next DIR_CHECK;
@@ -175,15 +172,6 @@ sub _serve_static {
     my $full_path = $c->_static_file;
     my $stat = stat $full_path;
 
-    # the below code all from C::P::Static
-    if ( $c->req->headers->if_modified_since ) {
-        if ( $c->req->headers->if_modified_since == $stat->mtime ) {
-            $c->res->status( 304 ); # Not Modified
-            $c->res->headers->remove_content_headers;
-            return 1;
-        }
-    }
-    
     $c->res->headers->content_type( $type );
     $c->res->headers->content_length( $stat->size );
     $c->res->headers->last_modified( $stat->mtime );
@@ -191,17 +179,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 +210,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)" )
@@ -346,11 +336,12 @@ For example:
 
 There are some file types you may not wish to serve as static files.  Most
 important in this category are your raw template files.  By default, files
-with the extensions tt, tt2, html, and xhtml will be ignored by Static::Simple
-in the interest of security.  If you wish to define your own extensions to
-ignore, use the ignore_extensions option:
+with the extensions tmpl, tt, tt2, html, and xhtml will be ignored by
+Static::Simple in the interest of security.  If you wish to define your own
+extensions to ignore, use the ignore_extensions option:
 
-    MyApp->config->{static}->{ignore_extensions} = [ qw/tt tt2 html xhtml/ ];
+    MyApp->config->{static}->{ignore_extensions} 
+        = [ qw/tmpl tt tt2 html xhtml/ ];
     
 =head2 Ignoring entire directories
 
@@ -381,17 +372,11 @@ you may enter your own extension to MIME type mapping.
         png => 'image/png',
     };
 
-=head2 Bypassing other plugins
+=head2 Compatibility with other plugins
 
-This plugin checks for a static file in the prepare_action stage.  If the
-request is for a static file, it will bypass all remaining prepare_action
-steps.  This means that by placing Static::Simple before all other plugins,
-they will not execute when a static file is found.  This can be helpful by
-skipping session cookie checks for example.  Or, if you want some plugins
-to run even on static files, list them before Static::Simple.
-
-Currently, work done by plugins in any other prepare method will execute
-normally.
+Since version 0.12, Static::Simple plays nice with other plugins.  It no
+longer short-circuits the prepare_action stage as it was causing too many
+compatibility issues with other plugins.
 
 =head2 Debugging information
 
@@ -412,6 +397,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>,