X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Static-Simple.git;a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FStatic%2FSimple.pm;h=ef6aee2e34ea6c0a25d56723f85a4605149bc241;hp=2cfe241233c029542fe6fbc698e1b6b3678979f1;hb=5c87f754780c60e8b0d05240a5b9e1673ce124d3;hpb=d925e93e969c28bd2d5634ebf1424dccea0db46c diff --git a/lib/Catalyst/Plugin/Static/Simple.pm b/lib/Catalyst/Plugin/Static/Simple.pm old mode 100644 new mode 100755 index 2cfe241..ef6aee2 --- a/lib/Catalyst/Plugin/Static/Simple.pm +++ b/lib/Catalyst/Plugin/Static/Simple.pm @@ -5,17 +5,27 @@ use File::stat; use File::Spec (); use IO::File (); use MIME::Types (); -use MRO::Compat; +use Catalyst::Utils; +use namespace::autoclean; -our $VERSION = '0.26'; +our $VERSION = '0.36'; has _static_file => ( is => 'rw' ); -has _static_debug_message => ( is => 'rw', isa => 'Str' ); +has _static_debug_message => ( is => 'rw', isa => 'ArrayRef[Str]' ); + +after setup_finalize => sub { + my $c = shift; + + # New: Turn off new 'autoflush' flag in logger (see Catalyst::Log). + # This is needed to surpress output of debug log messages for + # static requests: + $c->log->autoflush(0) if $c->log->can('autoflush'); +}; before prepare_action => sub { my $c = shift; my $path = $c->req->path; - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; @@ -53,25 +63,26 @@ before prepare_action => sub { } # Does the path have an extension? - if ( $path =~ /.*\.(\S{1,})$/xms ) { + if ( $path =~ /\.([^\/\\]+)$/m ) { # and does it exist? $c->_locate_static_file( $path ); } }; -override dispatch => sub { +around dispatch => sub { + my $orig = shift; my $c = shift; return if ( $c->res->status != 200 ); if ( $c->_static_file ) { - if ( $c->config->{static}{no_logs} && $c->log->can('abort') ) { + if ( $c->config->{'Plugin::Static::Simple'}->{no_logs} && $c->log->can('abort') ) { $c->log->abort( 1 ); } return $c->_serve_static; } else { - return super; + return $c->$orig(@_); } }; @@ -79,17 +90,31 @@ before finalize => sub { my $c = shift; # display all log messages - if ( $c->config->{static}{debug} && scalar @{$c->_debug_msg} ) { + if ( $c->config->{'Plugin::Static::Simple'}->{debug} && scalar @{$c->_debug_msg} ) { $c->log->debug( 'Static::Simple: ' . join q{ }, @{$c->_debug_msg} ); } }; -sub setup { +before setup_finalize => sub { my $c = shift; - $c->maybe::next::method(@_); + $c->log->warn("Deprecated 'static' config key used, please use the key 'Plugin::Static::Simple' instead") + if exists $c->config->{static}; - my $config = $c->config->{static} ||= {}; + if (exists $c->config->{static}->{include_path}) { + $c->config->{'Plugin::Static::Simple'}->{include_path} = [ + @{$c->config->{'Plugin::Static::Simple'}->{include_path} || []}, + @{delete $c->config->{static}->{include_path} || []} + ]; + } + + my $config + = $c->config->{'Plugin::Static::Simple'} + = $c->config->{'static'} + = Catalyst::Utils::merge_hashes( + $c->config->{'Plugin::Static::Simple'} || {}, + $c->config->{static} || {} + ); $config->{dirs} ||= []; $config->{include_path} ||= [ $c->config->{root} ]; @@ -103,10 +128,7 @@ sub setup { # load up a MIME::Types object, only loading types with # at least 1 file extension $config->{mime_types_obj} = MIME::Types->new( only_complete => 1 ); - - # preload the type index hash so it's not built on the first request - $config->{mime_types_obj}->create_type_index; -} +}; # Search through all included directories for the static file # Based on Template Toolkit INCLUDE_PATH code @@ -117,7 +139,7 @@ sub _locate_static_file { File::Spec->no_upwards( File::Spec->splitdir( $path ) ) ); - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; my @ipaths = @{ $config->{include_path} }; my $dpaths; my $count = 64; # maximum number of directories to search @@ -172,6 +194,7 @@ sub _locate_static_file { sub _serve_static { my $c = shift; + my $config = $c->config->{'Plugin::Static::Simple'}; my $full_path = shift || $c->_static_file; my $type = $c->_ext_to_type( $full_path ); @@ -180,6 +203,12 @@ sub _serve_static { $c->res->headers->content_type( $type ); $c->res->headers->content_length( $stat->size ); $c->res->headers->last_modified( $stat->mtime ); + # Tell Firefox & friends its OK to cache, even over SSL: + $c->res->headers->header('Cache-control' => 'public'); + # Optionally, set a fixed expiry time: + if ($config->{expires}) { + $c->res->headers->expires(time() + $config->{expires}); + } my $fh = IO::File->new( $full_path, 'r' ); if ( defined $fh ) { @@ -197,7 +226,7 @@ sub _serve_static { sub serve_static_file { my ( $c, $full_path ) = @_; - my $config = $c->config->{static} ||= {}; + my $config = $c->config->{'Plugin::Static::Simple'}; if ( -e $full_path ) { $c->_debug_msg( "Serving static file: $full_path" ) @@ -218,7 +247,7 @@ sub serve_static_file { sub _ext_to_type { my ( $c, $full_path ) = @_; - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; if ( $full_path =~ /.*\.(\S{1,})$/xms ) { my $ext = $1; @@ -292,11 +321,11 @@ properly. If the plugin can not find the file, the request is dispatched to your application instead. This means you are responsible for generating a -C<404> error if your applicaton can not process the request: +C<404> error if your application can not process the request: # handled by static::simple, not dispatched to your application /images/exists.png - + # static::simple will not find the file and let your application # handle the request. You are responsible for generating a file # or returning a 404 error @@ -307,7 +336,7 @@ the operation by adding various configuration options. In a production environment, you will probably want to use your webserver to deliver static content; for an example see L, below. -=head1 DEFAULT BEHAVIOR +=head1 DEFAULT BEHAVIOUR By default, Static::Simple will deliver all files having extensions (that is, bits of text following a period (C<.>)), I files @@ -329,7 +358,7 @@ Logging of static files is turned off by default. =head1 ADVANCED CONFIGURATION Configuration is completely optional and is specified within -Cconfig-E{static}>. If you use any of these options, +Cconfig-E{Plugin::Static::Simple}>. If you use any of these options, this module will probably feel less "simple" to you! =head2 Enabling request logging @@ -338,7 +367,7 @@ Since Catalyst 5.50, logging of static requests is turned off by default; static requests tend to clutter the log output and rarely reveal anything useful. However, if you want to enable logging of static requests, you can do so by setting -Cconfig-E{static}-E{logging}> to 1. +Cconfig-E{Plugin::Static::Simple}-E{logging}> to 1. =head2 Forcing directories into static mode @@ -347,7 +376,7 @@ that should always be served in static mode. Regular expressions may be specified using C. MyApp->config( - static => { + 'Plugin::Static::Simple' => { dirs => [ 'static', qr/^(images|css)/, @@ -364,7 +393,7 @@ added to the search path when you specify an C. You should use Cconfig-E{root}> to add it. MyApp->config( - static => { + 'Plugin::Static::Simple' => { include_path => [ '/path/to/overlay', \&incpath_generator, @@ -408,7 +437,7 @@ If you wish to define your own extensions to ignore, use the C option: MyApp->config( - static => { + 'Plugin::Static::Simple' => { ignore_extensions => [ qw/html asp php/ ], }, ); @@ -421,7 +450,7 @@ directory paths to ignore. If using C, the path will be checked against every included path. MyApp->config( - static => { + 'Plugin::Static::Simple' => { ignore_dirs => [ qw/tmpl css/ ], }, ); @@ -442,7 +471,7 @@ To override or add to the default MIME types set by the L module, you may enter your own extension to MIME type mapping. MyApp->config( - static => { + 'Plugin::Static::Simple' => { mime_types => { jpg => 'image/jpg', png => 'image/png', @@ -450,6 +479,23 @@ module, you may enter your own extension to MIME type mapping. }, ); +=head2 Controlling caching with Expires header + +The files served by Static::Simple will have a Last-Modified header set, +which allows some browsers to cache them for a while. However if you want +to explicitly set an Expires header, such as to allow proxies to cache your +static content, then you can do so by setting the "expires" config option. + +The value indicates the number of seconds after access time to allow caching. +So a value of zero really means "don't cache at all", and any higher values +will keep the file around for that long. + + MyApp->config( + 'Plugin::Static::Simple' => { + expires => 3600, # Caching allowed for one hour. + }, + ); + =head2 Compatibility with other plugins Since version 0.12, Static::Simple plays nice with other plugins. It no @@ -462,7 +508,7 @@ Enable additional debugging information printed in the Catalyst log. This is automatically enabled when running Catalyst in -Debug mode. MyApp->config( - static => { + 'Plugin::Static::Simple' => { debug => 1, }, ); @@ -547,6 +593,18 @@ messages. C initializes all default values. +=head1 DEPRECATIONS + +The old style of configuration using the C<'static'> config key was deprecated +in version 0.30. A warning will be issued if this is used, and the contents of +the config at this key will be merged with the newer C<'Plugin::Static::Simple'> +key. + +Be aware that if the C<'include_path'> key under C<'static'> exists at all, it +will be merged with any content of the same key under +C<'Plugin::Static::Simple'>. Be careful not to set this to a non-arrayref, +therefore. + =head1 SEE ALSO L, L, @@ -570,6 +628,10 @@ Tomas Doran, Justin Wheeler (dnm) +Matt S Trout, + +Toby Corkindale, + =head1 THANKS The authors of Catalyst::Plugin::Static: @@ -584,7 +646,7 @@ For the include_path code from Template Toolkit: =head1 COPYRIGHT -Copyright (c) 2005 - 2009 +Copyright (c) 2005 - 2011 the Catalyst::Plugin::Static::Simple L and L as listed above.