Changeset 28479 for lang/perl/Catalyst-Plugin-XSendFile
- Timestamp:
- 01/16/09 11:41:09 (4 years ago)
- Location:
- lang/perl/Catalyst-Plugin-XSendFile/trunk
- Files:
-
- 1 added
- 4 modified
-
Changes (modified) (1 diff)
-
Makefile.PL (modified) (3 diffs)
-
lib/Catalyst/Plugin/XSendFile.pm (modified) (10 diffs)
-
t/02_basic.t (modified) (3 diffs)
-
t/03_basic_oldstyle.t (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Catalyst-Plugin-XSendFile/trunk/Changes
r28476 r28479 1 1 Revision history for Perl module Catalyst::Plugin::XSendFile 2 3 0.03 4 - Refactor: 5 * no more adding methods just to use sendfile 6 * start adding support for non-lighttpd users with xsendfile capability 2 7 3 8 0.03_001 2006-12-19T19:44:16+09:00 -
lang/perl/Catalyst-Plugin-XSendFile/trunk/Makefile.PL
r28476 r28479 3 3 all_from 'lib/Catalyst/Plugin/XSendFile.pm'; 4 4 5 build_requires 'Test::More'; 5 test_requires 'Test::More'; 6 test_requires 'Test::Warn'; 6 7 7 8 requires 'Catalyst' => '5.6902'; … … 9 10 requires 'Class::Data::Inheritable' => 0; 10 11 requires 'File::Temp' => 0; 11 requires 'File::stat' => 0;12 12 requires 'NEXT' => 0; 13 13 requires 'Path::Class' => 0; … … 15 15 requires 'bytes' => 0; 16 16 17 use_test_base;18 17 auto_include; 19 18 WriteAll; -
lang/perl/Catalyst-Plugin-XSendFile/trunk/lib/Catalyst/Plugin/XSendFile.pm
r28476 r28479 6 6 use Catalyst::Utils; 7 7 use File::Temp qw/tempdir tempfile/; 8 use File::stat;9 8 use NEXT; 10 9 use Path::Class qw/file/; … … 16 15 =head1 NAME 17 16 18 Catalyst::Plugin::XSendFile - Catalyst plugin for lighttpd'sX-Sendfile.17 Catalyst::Plugin::XSendFile - Catalyst plugin for X-Sendfile. 19 18 20 19 =head1 SYNOPSIS 21 20 21 # In your MyApp.pm 22 22 use Catalyst qw/XSendFile/; 23 23 24 # manual send file 24 __PACKAGE__->config( 25 'Plugin::XSendFile' => { 26 frontend => 'lighttpd', # 'Apache' or 'lighttpd' 27 } 28 ); 29 30 # In your controller 25 31 sub show : Path('/files') { 26 32 my ( $self, $c, $filename ) = @_; … … 37 43 38 44 39 # auto using x-send-tempfile on large content serving 45 # auto using x-send-tempfile on large content serving (lighttpd only) 40 46 MyApp->config->{sendfile}{tempdir} = '/dev/shm'; 41 47 … … 60 66 instead of above. 61 67 62 But off -course you know, this feature doesn't work on Catalyst Test Server (myapp_server.pl).68 But off course, this feature doesn't work on Catalyst Test Server (myapp_server.pl). 63 69 So this module also provide its emulation when your app on test server. 64 70 … … 80 86 Faster - FastCGI 81 87 http://blog.lighttpd.net/articles/2006/11/29/faster-fastcgi 88 89 mod_xsendfile - X-Sendfile for Apache 90 http://tn123.ath.cx/mod_xsendfile/ (Old) 91 http://tn123.ath.cx/mod_xsendfile/beta/ (Beta, but supports Apache2.2) 82 92 83 93 =head1 NOTICE … … 103 113 $c->NEXT::setup(@_); 104 114 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; 110 134 111 135 $c; … … 121 145 my $c = shift; 122 146 147 my $res = $c->response; 148 my $config = $c->config->{'Plugin::XSendFile'}; 123 149 my $engine = $ENV{CATALYST_ENGINE} || ''; 124 150 125 151 # X-Sendfile emulation for test server. 126 152 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 ); 131 157 } 132 158 } 133 159 } 134 160 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} ); 138 163 139 164 if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { 140 my $s tat = stat $body;141 if ( $s tat and $stat->size >= 16*1024 ) {165 my $size = (stat($body))[7]; 166 if ( $size and $size >= 16*1024 ) { 142 167 while ( !eof $body ) { 143 168 read $body, my ($buffer), 4096; … … 147 172 close $fh; 148 173 149 $ c->res->send_tempfile($tempfile);174 $res->send_tempfile($tempfile); 150 175 } 151 176 } … … 154 179 $fh->close; 155 180 156 $ c->res->send_tempfile($tempfile);181 $res->send_tempfile($tempfile); 157 182 } 158 183 } … … 177 202 my ($self, $file) = @_; 178 203 $self->{body} = ''; 204 205 # default lighttpd, but if configured otherwise, we use 206 # x-sendfile 179 207 $self->header( 'X-LIGHTTPD-send-file' => $file ); 180 208 } -
lang/perl/Catalyst-Plugin-XSendFile/trunk/t/02_basic.t
r28476 r28479 3 3 use strict; 4 4 use warnings; 5 use Test::More (tests => 8); 5 6 6 7 use FindBin; … … 8 9 use lib File::Spec->catfile($FindBin::Bin, 'lib'); 9 10 10 use Test::Base;11 11 use Catalyst::Test 'TestApp'; 12 12 use File::Temp qw/tempdir/; 13 13 14 plan tests => 8; 15 16 TestApp->config->{sendfile}{tempdir} = tempdir( CLEANUP => 1 ); 14 TestApp->config->{'Plugin::XSendFile'}{tempdir} = tempdir( CLEANUP => 1 ); 17 15 TestApp->setup; 18 16 … … 24 22 is( $res->header('X-LIGHTTPD-send-file'), $image_fn, 'correct sendfile header'); 25 23 26 # sendfile: lighty emu ration24 # sendfile: lighty emulation 27 25 { 28 26 local $ENV{CATALYST_ENGINE} = 'HTTP';
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)