From: Andy Grundman Date: Fri, 24 Mar 2006 16:13:11 +0000 (+0000) Subject: Static::Simple 0.14, fix for files with spaces X-Git-Tag: v0.17~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Static-Simple.git;a=commitdiff_plain;h=792411e602ff6b5f70844518aca6a9792fe3c22a Static::Simple 0.14, fix for files with spaces --- diff --git a/Changes b/Changes index 55d0e09..71b4718 100644 --- 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 diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index 4d0c740..d579be0 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -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 diff --git a/META.yml b/META.yml index b7418c3..9cdc21f 100644 --- 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 Grundman, 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 diff --git a/lib/Catalyst/Plugin/Static/Simple.pm b/lib/Catalyst/Plugin/Static/Simple.pm index 2800056..ea7de72 100644 --- a/lib/Catalyst/Plugin/Static/Simple.pm +++ b/lib/Catalyst/Plugin/Static/Simple.pm @@ -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} diff --git a/t/04static.t b/t/04static.t index edb8974..ee6bcbd 100644 --- a/t/04static.t +++ b/t/04static.t @@ -6,7 +6,11 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 7; +# Module::Build craps out on files with spaces so it's not included in the dist +my $has_space_file = -e "$FindBin::Bin/lib/TestApp/root/files/space file.txt"; + +use Test::More; +plan tests => ($has_space_file) ? 10 : 7; use Catalyst::Test 'TestApp'; # test getting a css file @@ -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' ); like( $res->content, qr/background/, 'content of css ok' ); +# test a file with spaces +if ( $has_space_file ) { + ok( $res = request('http://localhost/files/space file.txt'), 'request ok' ); + is( $res->content_type, 'text/plain', 'content-type text/plain ok' ); + like( $res->content, qr/background/, 'content of space file ok' ); +} + # test a non-existent file ok( $res = request('http://localhost/files/404.txt'), 'request ok' ); is( $res->content, 'default', 'default handler for non-existent content ok' ); diff --git a/t/lib/TestApp/root/files/space file.txt b/t/lib/TestApp/root/files/space file.txt new file mode 100644 index 0000000..de57cdb --- /dev/null +++ b/t/lib/TestApp/root/files/space file.txt @@ -0,0 +1,3 @@ +body { + background: #fff; +}