[Catalyst-commits] r11621 - in trunk/Catalyst-Plugin-Static: . lib
lib/Catalyst lib/Catalyst/Plugin t t/lib
t0m at dev.catalyst.perl.org
t0m at dev.catalyst.perl.org
Sun Oct 18 17:59:11 GMT 2009
Author: t0m
Date: 2009-10-18 17:59:11 +0000 (Sun, 18 Oct 2009)
New Revision: 11621
Added:
trunk/Catalyst-Plugin-Static/lib/
trunk/Catalyst-Plugin-Static/lib/Catalyst/
trunk/Catalyst-Plugin-Static/lib/Catalyst/Plugin/
trunk/Catalyst-Plugin-Static/lib/Catalyst/Plugin/Static.pm
Removed:
trunk/Catalyst-Plugin-Static/Static.pm
Modified:
trunk/Catalyst-Plugin-Static/Makefile.PL
trunk/Catalyst-Plugin-Static/t/02static.t
trunk/Catalyst-Plugin-Static/t/lib/TestApp.pm
Log:
Fix tests
Modified: trunk/Catalyst-Plugin-Static/Makefile.PL
===================================================================
--- trunk/Catalyst-Plugin-Static/Makefile.PL 2009-10-18 17:48:13 UTC (rev 11620)
+++ trunk/Catalyst-Plugin-Static/Makefile.PL 2009-10-18 17:59:11 UTC (rev 11621)
@@ -8,5 +8,5 @@
'File::MimeInfo' => '0.11',
'File::Slurp' => 0
},
- VERSION_FROM => 'Static.pm'
+ VERSION_FROM => 'lib/Catalyst/Plugin/Static.pm'
);
Deleted: trunk/Catalyst-Plugin-Static/Static.pm
===================================================================
--- trunk/Catalyst-Plugin-Static/Static.pm 2009-10-18 17:48:13 UTC (rev 11620)
+++ trunk/Catalyst-Plugin-Static/Static.pm 2009-10-18 17:59:11 UTC (rev 11621)
@@ -1,139 +0,0 @@
-package Catalyst::Plugin::Static;
-
-use strict;
-use base 'Class::Data::Inheritable';
-use File::MimeInfo::Magic;
-use File::stat;
-use File::Slurp;
-use File::Spec::Functions qw/catdir no_upwards splitdir/;
-use NEXT;
-
-our $VERSION = '0.10';
-
-
-=head1 NAME
-
-Catalyst::Plugin::Static - Serve static files with Catalyst
-
-=head1 SYNOPSIS
-
- use Catalyst 'Static';
-
- # let File::MMagic determine the content type
- $c->serve_static;
-
- # or specify explicitly if you know better
- $c->serve_static('text/css');
-
-=head1 DESCRIPTION
-
-Serve static files from config->{root}. Note that for most purposes, you'll
-probably want to use L<Catalyst::Plugin::Static::Simple> rather than this
-one.
-
-=head2 METHODS
-
-=over 4
-
-=item finalize
-
-This plugin overrides finalize to make sure content is removed on
-redirect.
-
-=cut
-
-sub finalize {
- my $c = shift;
- if ( $c->res->status =~ /^(1\d\d|[23]04)$/ ) {
- $c->res->headers->remove_content_headers;
- return $c->finalize_headers;
- }
- return $c->NEXT::finalize(@_);
-
-}
-
-=item serve_static
-
-Call this method from your action to serve requested path
-as a static file from your root. takes an optional content_type
-parameter
-
-=cut
-
-sub serve_static {
- my $c = shift;
- my $path = $c->config->{root} . '/' . $c->req->path;
- return $c->serve_static_file( $path, @_ );
-}
-
-=item serve_static_file <file>
-
-Serve a specified static file.
-
-=cut
-
-sub serve_static_file {
- my $c = shift;
- my $path = catdir(no_upwards(splitdir( shift )));
-
- if ( -f $path ) {
-
- my $stat = stat($path);
-
- if ( $c->req->headers->header('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;
- }
- }
-
- my $type = shift || mimetype($path);
- my $content = read_file($path);
- $c->res->headers->content_type($type);
- $c->res->headers->content_length( $stat->size );
- $c->res->headers->last_modified( $stat->mtime );
- $c->res->output($content);
- if ( $c->config->{static}->{no_logs} && $c->log->can('abort') ) {
- $c->log->abort( 1 );
- }
- $c->log->debug(qq/Serving file "$path" as "$type"/) if $c->debug;
- return 1;
- }
-
- $c->log->debug(qq/Failed to serve file "$path"/) if $c->debug;
- $c->res->status(404);
-
- return 0;
-}
-
-=back
-
-=head1 SEE ALSO
-
-L<Catalyst>.
-
-=head1 CAVEATS
-
-This module is not as optimized for static files as a normal web
-server, and is most useful for stand alone operation and development.
-
-=head1 AUTHOR
-
-Sebastian Riedel, C<sri at cpan.org>
-Christian Hansen <ch at ngmedia.com>
-Marcus Ramberg <mramberg at cpan.org>
-
-=head1 THANK YOU
-
-Torsten Seemann and all the others who've helped.
-
-=head1 COPYRIGHT
-
-This program is free software, you can redistribute it and/or modify it under
-the same terms as Perl itself.
-
-=cut
-
-1;
Added: trunk/Catalyst-Plugin-Static/lib/Catalyst/Plugin/Static.pm
===================================================================
--- trunk/Catalyst-Plugin-Static/lib/Catalyst/Plugin/Static.pm (rev 0)
+++ trunk/Catalyst-Plugin-Static/lib/Catalyst/Plugin/Static.pm 2009-10-18 17:59:11 UTC (rev 11621)
@@ -0,0 +1,143 @@
+package Catalyst::Plugin::Static;
+
+use strict;
+use base 'Class::Data::Inheritable';
+use File::MimeInfo::Magic;
+use File::stat;
+use File::Slurp;
+use File::Spec::Functions qw/catdir no_upwards splitdir/;
+use NEXT;
+
+our $VERSION = '0.10';
+
+
+=head1 NAME
+
+Catalyst::Plugin::Static - Serve static files with Catalyst
+
+=head1 SYNOPSIS
+
+ use Catalyst 'Static';
+
+ # let File::MMagic determine the content type
+ $c->serve_static;
+
+ # or specify explicitly if you know better
+ $c->serve_static('text/css');
+
+=head1 DESCRIPTION
+
+Serve static files from config->{root}. Note that for most purposes, you'll
+probably want to use L<Catalyst::Plugin::Static::Simple> rather than this
+one.
+
+=head2 METHODS
+
+=over 4
+
+=item finalize
+
+This plugin overrides finalize to make sure content is removed on
+redirect.
+
+=cut
+
+sub finalize {
+ my $c = shift;
+ if ( $c->res->status =~ /^(1\d\d|[23]04)$/ ) {
+ $c->res->headers->remove_content_headers;
+ $c->finalize_headers;
+ }
+ return $c->NEXT::finalize(@_);
+
+}
+
+=item serve_static
+
+Call this method from your action to serve requested path
+as a static file from your root. takes an optional content_type
+parameter
+
+=cut
+
+sub serve_static {
+ my $c = shift;
+ my $r = eval {
+ my $path = $c->config->{root} . '/' . $c->req->path;
+ $c->serve_static_file( $path, @_ );
+ };
+ warn("serve_static puked $@") if $@;
+ $r;
+}
+
+=item serve_static_file <file>
+
+Serve a specified static file.
+
+=cut
+
+sub serve_static_file {
+ my $c = shift;
+ my $path = catdir(no_upwards(splitdir( shift )));
+
+ if ( -f $path ) {
+
+ my $stat = stat($path);
+
+ if ( $c->req->headers->header('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;
+ }
+ }
+
+ my $type = shift || mimetype($path);
+ my $content = read_file($path);
+ $c->res->headers->content_type($type);
+ $c->res->headers->content_length( $stat->size );
+ $c->res->headers->last_modified( $stat->mtime );
+ $c->res->output($content);
+ if ( $c->config->{static}->{no_logs} && $c->log->can('abort') ) {
+ $c->log->abort( 1 );
+ }
+ $c->log->debug(qq/Serving file "$path" as "$type"/) if $c->debug;
+ return 1;
+ }
+
+ $c->log->debug(qq/Failed to serve file "$path"/) if $c->debug;
+ $c->res->status(404);
+
+ return 0;
+}
+
+=back
+
+=head1 SEE ALSO
+
+L<Catalyst>.
+
+=head1 CAVEATS
+
+This module is not as optimized for static files as a normal web
+server, and is most useful for stand alone operation and development.
+
+=head1 AUTHOR
+
+Sebastian Riedel, C<sri at cpan.org>
+Christian Hansen <ch at ngmedia.com>
+Marcus Ramberg <mramberg at cpan.org>
+
+=head1 THANK YOU
+
+Torsten Seemann and all the others who've helped.
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
+1;
Modified: trunk/Catalyst-Plugin-Static/t/02static.t
===================================================================
--- trunk/Catalyst-Plugin-Static/t/02static.t 2009-10-18 17:48:13 UTC (rev 11620)
+++ trunk/Catalyst-Plugin-Static/t/02static.t 2009-10-18 17:59:11 UTC (rev 11621)
@@ -26,7 +26,8 @@
);
ok( my $response = request($request), 'If Modified Since request' );
- is( $response->code , 304, 'Not Modified status code' );
+ use Data::Dumper;
+ is( $response->code , 304, 'Not Modified status code' ) or warn Dumper($response);
is( $response->content , '', 'No content' );
}
Modified: trunk/Catalyst-Plugin-Static/t/lib/TestApp.pm
===================================================================
--- trunk/Catalyst-Plugin-Static/t/lib/TestApp.pm 2009-10-18 17:48:13 UTC (rev 11620)
+++ trunk/Catalyst-Plugin-Static/t/lib/TestApp.pm 2009-10-18 17:59:11 UTC (rev 11621)
@@ -1,17 +1,20 @@
package TestApp;
+use strict;
+use warnings;
-use Catalyst qw[-Engine=Test Static];
+use Catalyst qw[Static];
use File::Spec::Functions qw[catpath splitpath rel2abs];
__PACKAGE__->config(
root => rel2abs( catpath( ( splitpath($0) )[0,1], '' ) )
);
- sub default : Private {
- my ( $self, $c ) = @_;
- $c->serve_static;
- }
+sub default : Private {
+ my ( $self, $c ) = @_;
+ $c->serve_static;
+}
__PACKAGE__->setup();
1;
+
More information about the Catalyst-commits
mailing list