Catalyst::Plugin::XSendFile - Catalyst plugin for lighttpd's X-Sendfile.


Catalyst-Plugin-XSendFile documentation Contained in the Catalyst-Plugin-XSendFile distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::XSendFile - Catalyst plugin for lighttpd's X-Sendfile.

SYNOPSIS

Top

    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" );
        }
    }

DESCRIPTION

Top

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.

SEE ALSO

Top

lighty's life - X-Sendfile http://blog.lighttpd.net/articles/2006/07/02/x-sendfile

NOTICE

Top

To use it you have to set "allow-x-sendfile" option enabled in your fastcgi configuration.

    "allow-x-send-file" => "enable",

EXTENDED_METHODS

Top

setup

Setup MIME::Types object unless Static::Simple loaded.

finalize_headers

Added X-Sendfile emulation feature for test server.

EXTENDED_RESPONSE_METHODS

Top

sendfile

Set X-LIGHTTPD-send-file header easily.

AUTHOR

Top

Daisuke Murase <typester@cpan.org>

COPYRIGHT

Top


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;