| Catalyst-Plugin-XSendFile documentation | Contained in the Catalyst-Plugin-XSendFile distribution. |
Catalyst::Plugin::XSendFile - Catalyst plugin for lighttpd's X-Sendfile.
use Catalyst qw/XSendFile/;
sub show : Path('/files') {
my ( $self, $c, $filename ) = @_;
# unless login, it shows 403 forbidden screen
$c->res->status(403);
$c->stash->{template} = 'error-403.tt';
# serving a static file only when user logged in.
if ($c->user) {
$c->res->sendfile( "/path/to/$filename" );
}
}
lighty's X-Sendfile feature is great.
If you use lighttpd + fastcgi, you can show files only set X-Sendfile header like below:
$c->res->header( 'X-LIGHTTPD-send-file' => $filename );
This feature is especially great for serving static file on authentication area.
And with this plugin, you can use:
$c->res->sendfile( $filename );
instead of above.
But off-course you know, this feature doesn't work on Catalyst Test Server (myapp_server.pl). So this module also provide its emulation when your app on test server.
lighty's life - X-Sendfile http://blog.lighttpd.net/articles/2006/07/02/x-sendfile
To use it you have to set "allow-x-sendfile" option enabled in your fastcgi configuration.
"allow-x-send-file" => "enable",
Setup MIME::Types object unless Static::Simple loaded.
Added X-Sendfile emulation feature for test server.
Set X-LIGHTTPD-send-file header easily.
Daisuke Murase <typester@cpan.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
| Catalyst-Plugin-XSendFile documentation | Contained in the Catalyst-Plugin-XSendFile distribution. |
package Catalyst::Plugin::XSendFile; use strict; use warnings; use base qw/Class::Data::Inheritable/; use NEXT; use MIME::Types; use Path::Class qw/file/; our $VERSION = '0.02';
sub setup { my $c = shift; $c->NEXT::setup(@_); unless ( $c->registered_plugins('Static::Simple') ) { __PACKAGE__->mk_classdata( _static_mime_types => MIME::Types->new( only_complete => 1 ) ); __PACKAGE__->_static_mime_types->create_type_index; } $c; }
sub finalize_headers { my $c = shift; # X-Sendfile emulation for test server. if ( ($ENV{CATALYST_ENGINE} || '') =~ /^HTTP/ ) { if ( my $sendfile = file( $c->res->header('X-LIGHTTPD-send-file') ) ) { $c->res->headers->remove_header('X-LIGHTTPD-send-file'); if ( $sendfile->stat && -f _ && -r _ ) { my ($ext) = $sendfile =~ /\.(.+?)$/; my $user_types = $c->config->{static}->{mime_types}; my $mime_type = $user_types->{$ext} || $c->_static_mime_types->mimeTypeOf($ext); $c->res->status(200); $c->res->content_type($mime_type); $c->res->body( $sendfile->openr ); } } } $c->NEXT::finalize_headers; }
{ package Catalyst::Response; sub sendfile { my ($self, $file) = @_; $self->header( 'X-LIGHTTPD-send-file' => $file ); } }
1;