Static::Simple 0.14, fix for files with spaces
Andy Grundman [Fri, 24 Mar 2006 16:13:11 +0000 (16:13 +0000)]
Changes
MANIFEST.SKIP
META.yml
lib/Catalyst/Plugin/Static/Simple.pm
t/04static.t
t/lib/TestApp/root/files/space file.txt [new file with mode: 0644]

diff --git a/Changes b/Changes
index 55d0e09..71b4718 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
 Revision history for Perl extension Catalyst::Plugin::Static::Simple
 
+0.14    2006-03-24 11:15:00
+        - Unescape the URI path before looking for the file.  This fixes
+                 issues with files that have spaces.
+
 0.13    2005-12-15 10:00:00
         - Fixed bug in ignore_dirs under win32.
         - Doc rewriting
index 4d0c740..d579be0 100644 (file)
@@ -27,3 +27,5 @@
 # Don't ship the test db
 ^t/var
 
+# Module::Build can't handle files with spaces
+t/lib/TestApp/root/files/space file.txt
index b7418c3..9cdc21f 100644 (file)
--- a/META.yml
+++ b/META.yml
@@ -1,15 +1,8 @@
----
+--- #YAML:1.0
 name: Catalyst-Plugin-Static-Simple
-version: 0.13
+version: 0.14
 author:
-  - 'Andy Grundman, <andy@hybridized.org>'
+  - Andy Grundman, <andy@hybridized.org>
 abstract: Make serving static pages painless.
 license: perl
-requires:
-  Catalyst: 5.30
-  MIME::Types: 1.15
-provides:
-  Catalyst::Plugin::Static::Simple:
-    file: lib/Catalyst/Plugin/Static/Simple.pm
-    version: 0.13
-generated_by: Module::Build version 0.2611
+generated_by: Module::Build version 0.2612, without YAML.pm
index 2800056..ea7de72 100644 (file)
@@ -9,7 +9,7 @@ use IO::File;
 use MIME::Types;
 use NEXT;
 
-our $VERSION = '0.13';
+our $VERSION = '0.14';
 
 __PACKAGE__->mk_classdata( qw/_static_mime_types/ );
 __PACKAGE__->mk_accessors( qw/_static_file
@@ -18,6 +18,8 @@ __PACKAGE__->mk_accessors( qw/_static_file
 sub prepare_action {
     my $c = shift;
     my $path = $c->req->path;
+    
+    $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
 
     # is the URI in a static-defined path?
     foreach my $dir ( @{ $c->config->{static}->{dirs} } ) {
@@ -26,7 +28,7 @@ sub prepare_action {
             $c->error( "Error compiling static dir regex '$dir': $@" );
         }
         if ( $path =~ $re ) {
-            if ( $c->_locate_static_file ) {
+            if ( $c->_locate_static_file( $path ) ) {
                 $c->_debug_msg( 'from static directory' )
                     if ( $c->config->{static}->{debug} );
             } else {
@@ -40,7 +42,7 @@ sub prepare_action {
     # Does the path have an extension?
     if ( $path =~ /.*\.(\S{1,})$/xms ) {
         # and does it exist?
-        $c->_locate_static_file;
+        $c->_locate_static_file( $path );
     }
     
     return $c->NEXT::ACTUAL::prepare_action(@_);
@@ -109,9 +111,9 @@ sub setup {
 # Search through all included directories for the static file
 # Based on Template Toolkit INCLUDE_PATH code
 sub _locate_static_file {
-    my $c = shift;
+    my ( $c, $path ) = @_;
     
-    my $path = catdir( no_upwards( splitdir( $c->req->path ) ) );
+    $path = catdir( no_upwards( splitdir( $path ) ) );
     
     my @ipaths = @{ $c->config->{static}->{include_path} };
     my $dpaths;
@@ -165,12 +167,10 @@ sub _locate_static_file {
 
 sub _serve_static {
     my $c = shift;
-    
-    my $path = $c->req->path;    
-    my $type = $c->_ext_to_type;
-    
+           
     my $full_path = $c->_static_file;
-    my $stat = stat $full_path;
+    my $type      = $c->_ext_to_type( $full_path );
+    my $stat      = stat $full_path;
 
     $c->res->headers->content_type( $type );
     $c->res->headers->content_length( $stat->size );
@@ -199,10 +199,9 @@ sub _serve_static {
 
 # looks up the correct MIME type for the current file extension
 sub _ext_to_type {
-    my $c = shift;
-    my $path = $c->req->path;
+    my ( $c, $full_path ) = @_;
     
-    if ( $path =~ /.*\.(\S{1,})$/xms ) {
+    if ( $full_path =~ /.*\.(\S{1,})$/xms ) {
         my $ext = $1;
         my $user_types = $c->config->{static}->{mime_types};
         my $type = $user_types->{$ext} 
index edb8974..ee6bcbd 100644 (file)
@@ -6,7 +6,11 @@ use warnings;
 use FindBin;\r
 use lib "$FindBin::Bin/lib";\r
 \r
-use Test::More tests => 7;\r
+# Module::Build craps out on files with spaces so it's not included in the dist\r
+my $has_space_file = -e "$FindBin::Bin/lib/TestApp/root/files/space file.txt";\r
+\r
+use Test::More;\r
+plan tests => ($has_space_file) ? 10 : 7;\r
 use Catalyst::Test 'TestApp';\r
 \r
 # test getting a css file\r
@@ -14,6 +18,13 @@ ok( my $res = request('http://localhost/files/static.css'), 'request ok' );
 is( $res->content_type, 'text/css', 'content-type text/css ok' );\r
 like( $res->content, qr/background/, 'content of css ok' );\r
 \r
+# test a file with spaces\r
+if ( $has_space_file ) {\r
+    ok( $res = request('http://localhost/files/space file.txt'), 'request ok' );\r
+    is( $res->content_type, 'text/plain', 'content-type text/plain ok' );\r
+    like( $res->content, qr/background/, 'content of space file ok' );\r
+}\r
+\r
 # test a non-existent file\r
 ok( $res = request('http://localhost/files/404.txt'), 'request ok' );\r
 is( $res->content, 'default', 'default handler for non-existent content ok' );\r
diff --git a/t/lib/TestApp/root/files/space file.txt b/t/lib/TestApp/root/files/space file.txt
new file mode 100644 (file)
index 0000000..de57cdb
--- /dev/null
@@ -0,0 +1,3 @@
+body {
+    background: #fff;
+}