Show
Ignore:
Timestamp:
01/16/09 11:41:09 (4 years ago)
Author:
daisuke
Message:

start refactoring

Location:
lang/perl/Catalyst-Plugin-XSendFile/trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Catalyst-Plugin-XSendFile/trunk/Changes

    r28476 r28479  
    11Revision history for Perl module Catalyst::Plugin::XSendFile 
     2 
     30.03 
     4    - Refactor:  
     5        * no more adding methods just to use sendfile 
     6        * start adding support for non-lighttpd users with xsendfile capability 
    27 
    380.03_001 2006-12-19T19:44:16+09:00 
  • lang/perl/Catalyst-Plugin-XSendFile/trunk/Makefile.PL

    r28476 r28479  
    33all_from 'lib/Catalyst/Plugin/XSendFile.pm'; 
    44 
    5 build_requires 'Test::More'; 
     5test_requires 'Test::More'; 
     6test_requires 'Test::Warn'; 
    67 
    78requires 'Catalyst'                 => '5.6902'; 
     
    910requires 'Class::Data::Inheritable' => 0; 
    1011requires 'File::Temp'               => 0; 
    11 requires 'File::stat'               => 0; 
    1212requires 'NEXT'                     => 0; 
    1313requires 'Path::Class'              => 0; 
     
    1515requires 'bytes'                    => 0; 
    1616 
    17 use_test_base; 
    1817auto_include; 
    1918WriteAll; 
  • lang/perl/Catalyst-Plugin-XSendFile/trunk/lib/Catalyst/Plugin/XSendFile.pm

    r28476 r28479  
    66use Catalyst::Utils; 
    77use File::Temp qw/tempdir tempfile/; 
    8 use File::stat; 
    98use NEXT; 
    109use Path::Class qw/file/; 
     
    1615=head1 NAME 
    1716 
    18 Catalyst::Plugin::XSendFile - Catalyst plugin for lighttpd's X-Sendfile. 
     17Catalyst::Plugin::XSendFile - Catalyst plugin for X-Sendfile. 
    1918 
    2019=head1 SYNOPSIS 
    2120 
     21    # In your MyApp.pm 
    2222    use Catalyst qw/XSendFile/; 
    2323     
    24     # manual send file 
     24    __PACKAGE__->config( 
     25        'Plugin::XSendFile' => { 
     26            frontend => 'lighttpd', # 'Apache' or 'lighttpd' 
     27        } 
     28    ); 
     29 
     30    # In your controller 
    2531    sub show : Path('/files') { 
    2632        my ( $self, $c, $filename ) = @_; 
     
    3743     
    3844     
    39     # auto using x-send-tempfile on large content serving 
     45    # auto using x-send-tempfile on large content serving (lighttpd only) 
    4046    MyApp->config->{sendfile}{tempdir} = '/dev/shm'; 
    4147 
     
    6066instead of above. 
    6167 
    62 But off-course you know, this feature doesn't work on Catalyst Test Server (myapp_server.pl). 
     68But off course, this feature doesn't work on Catalyst Test Server (myapp_server.pl). 
    6369So this module also provide its emulation when your app on test server. 
    6470 
     
    8086Faster - FastCGI 
    8187http://blog.lighttpd.net/articles/2006/11/29/faster-fastcgi 
     88 
     89mod_xsendfile - X-Sendfile for Apache 
     90http://tn123.ath.cx/mod_xsendfile/ (Old) 
     91http://tn123.ath.cx/mod_xsendfile/beta/ (Beta, but supports Apache2.2) 
    8292 
    8393=head1 NOTICE 
     
    103113    $c->NEXT::setup(@_); 
    104114 
    105     my $tempdir = $c->config->{sendfile}{tempdir} 
    106       || Catalyst::Utils::class2tempdir($c, 1); 
    107  
    108     __PACKAGE__->mk_classdata( 
    109         _sendfile_tempdir => tempdir( DIR => $tempdir, CLEANUP => 1 ) ); 
     115    my $config  = $c->config->{'Plugin::XSendFile'}; 
     116    if (! $config && ($config = $c->config->{sendfile})) { 
     117        warn "Old-style configuration detected. Please use 'Plugin::XSendFile' key instead of 'sendfile'"; 
     118        delete $c->config->{sendfile}; 
     119    } 
     120 
     121    $config ||= {}; 
     122 
     123    my $frontend = $config->{frontend} || 'lighttpd'; 
     124    if ($frontend =~ /^lighttpd$/i) { 
     125        $config->{sendfile_header} ||= 'X-LIGHTTPD-send-file'; 
     126        $config->{sendfile_temp_header} ||= 'X-LIGHTTPD-send-tempfile'; 
     127    } else { 
     128        $config->{sendfile_header} ||= 'X-SENDFILE'; 
     129        $config->{sendfile_temp_header} ||= ''; 
     130    } 
     131 
     132    $config->{tempdir} ||= Catalyst::Utils::class2tempdir($c, 1); 
     133    $c->config->{'Plugin::XSendFile'} = $config; 
    110134 
    111135    $c; 
     
    121145    my $c = shift; 
    122146 
     147    my $res    = $c->response; 
     148    my $config = $c->config->{'Plugin::XSendFile'}; 
    123149    my $engine = $ENV{CATALYST_ENGINE} || ''; 
    124150 
    125151    # X-Sendfile emulation for test server. 
    126152    if ( $engine =~ /^HTTP/ ) { 
    127         if ( my $sendfile = file( $c->res->header('X-LIGHTTPD-send-file') ) ) { 
    128             $c->res->headers->remove_header('X-LIGHTTPD-send-file'); 
    129             if ( $sendfile->stat && -f _ && -r _ ) { 
    130                 $c->res->body( $sendfile->openr ); 
     153        if ( my $sendfile = file( $res->header('X-LIGHTTPD-send-file') ) ) { 
     154            $res->headers->remove_header('X-LIGHTTPD-send-file'); 
     155            if ( stat($sendfile) && -f _ && -r _ ) { 
     156                $res->body( $sendfile->openr ); 
    131157            } 
    132158        } 
    133159    } 
    134160    elsif ( $engine eq 'FastCGI' ) { 
    135  
    136         if ( my $body = $c->res->body ) { 
    137             my ( $fh, $tempfile ) = tempfile( DIR => $c->_sendfile_tempdir ); 
     161        if ( my $body = $res->body ) { 
     162            my ( $fh, $tempfile ) = tempfile( DIR => $config->{tempdir} ); 
    138163 
    139164            if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { 
    140                 my $stat = stat $body; 
    141                 if ( $stat and $stat->size >= 16*1024 ) { 
     165                my $size = (stat($body))[7]; 
     166                if ( $size and $size >= 16*1024 ) { 
    142167                    while ( !eof $body ) { 
    143168                        read $body, my ($buffer), 4096; 
     
    147172                    close $fh; 
    148173 
    149                     $c->res->send_tempfile($tempfile); 
     174                    $res->send_tempfile($tempfile); 
    150175                } 
    151176            } 
     
    154179                $fh->close; 
    155180 
    156                 $c->res->send_tempfile($tempfile); 
     181                $res->send_tempfile($tempfile); 
    157182            } 
    158183        } 
     
    177202        my ($self, $file) = @_; 
    178203        $self->{body} = ''; 
     204 
     205        # default lighttpd, but if configured otherwise, we use 
     206        # x-sendfile 
    179207        $self->header( 'X-LIGHTTPD-send-file' => $file ); 
    180208    } 
  • lang/perl/Catalyst-Plugin-XSendFile/trunk/t/02_basic.t

    r28476 r28479  
    33use strict; 
    44use warnings; 
     5use Test::More (tests => 8); 
    56 
    67use FindBin; 
     
    89use lib File::Spec->catfile($FindBin::Bin, 'lib'); 
    910 
    10 use Test::Base; 
    1111use Catalyst::Test 'TestApp'; 
    1212use File::Temp qw/tempdir/; 
    1313 
    14 plan tests => 8; 
    15  
    16 TestApp->config->{sendfile}{tempdir} = tempdir( CLEANUP => 1 ); 
     14TestApp->config->{'Plugin::XSendFile'}{tempdir} = tempdir( CLEANUP => 1 ); 
    1715TestApp->setup; 
    1816 
     
    2422is( $res->header('X-LIGHTTPD-send-file'), $image_fn, 'correct sendfile header'); 
    2523 
    26 # sendfile: lighty emuration 
     24# sendfile: lighty emulation 
    2725{ 
    2826    local $ENV{CATALYST_ENGINE} = 'HTTP';