| 1 | package Plack::App::CGIBin; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use parent qw/Plack::App::File/; | ||
| 5 | use Plack::Util::Accessor qw( exec_cb ); | ||
| 6 | use Plack::App::WrapCGI; | ||
| 7 | |||
| 8 | sub allow_path_info { 1 } | ||
| 9 | |||
| 10 | my %exec_cache; | ||
| 11 | |||
| 12 | sub would_exec { | ||
| 13 | my($self, $file) = @_; | ||
| 14 | |||
| 15 | return $exec_cache{$file} if exists $exec_cache{$file}; | ||
| 16 | |||
| 17 | my $exec_cb = $self->exec_cb || sub { $self->exec_cb_default(@_) }; | ||
| 18 | |||
| 19 | return $exec_cache{$file} = $exec_cb->($file); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub exec_cb_default { | ||
| 23 | my($self, $file) = @_; | ||
| 24 | |||
| 25 | if ($file =~ /\.pl$/i) { | ||
| 26 | return 0; | ||
| 27 | } elsif ($self->shebang_for($file) =~ /^\#\!.*perl/) { | ||
| 28 | return 0; | ||
| 29 | } else { | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | sub shebang_for { | ||
| 35 | my($self, $file) = @_; | ||
| 36 | |||
| 37 | open my $fh, "<", $file or return ''; | ||
| 38 | my $line = <$fh>; | ||
| 39 | return $line; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub serve_path { | ||
| 43 | my($self, $env, $file) = @_; | ||
| 44 | |||
| 45 | local @{$env}{qw(SCRIPT_NAME PATH_INFO)} = @{$env}{qw( plack.file.SCRIPT_NAME plack.file.PATH_INFO )}; | ||
| 46 | |||
| 47 | my $app = $self->{_compiled}->{$file} ||= Plack::App::WrapCGI->new( | ||
| 48 | script => $file, execute => $self->would_exec($file), | ||
| 49 | )->to_app; | ||
| 50 | $app->($env); | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |