----
+--- #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
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
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} } ) {
$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 {
# 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(@_);
# 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;
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 );
# 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}
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
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