C::C::CGIBin - added test for __DATA__ (failing for some reaosn)
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
CommitLineData
21a20b7e 1package Catalyst::Controller::CGIBin;
2
3use strict;
4use warnings;
5
1ce18a56 6use MRO::Compat;
7use mro 'c3';
21a20b7e 8use File::Slurp 'slurp';
9use File::Find::Rule ();
21a20b7e 10use Catalyst::Exception ();
c264816e 11use File::Spec::Functions qw/splitdir abs2rel/;
12use IPC::Open3;
13use Symbol 'gensym';
14use List::MoreUtils 'any';
15use IO::File ();
1ce18a56 16use Carp;
2340af9d 17use namespace::clean -except => 'meta';
21a20b7e 18
19use parent 'Catalyst::Controller::WrapCGI';
20
21=head1 NAME
22
23Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
24
25=head1 VERSION
26
fbaba9dd 27Version 0.007
21a20b7e 28
29=cut
30
fbaba9dd 31our $VERSION = '0.007';
21a20b7e 32
33=head1 SYNOPSIS
34
35In your controller:
36
37 package MyApp::Controller::Foo;
38
39 use parent qw/Catalyst::Controller::CGIBin/;
40
41 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
42 sub dongs : Local Args(0) {
43 my ($self, $c) = @_;
44 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
45 }
46
47In your .conf:
48
49 <Controller::Foo>
50 <CGI>
51 username_field username # used for REMOTE_USER env var
52 pass_env PERL5LIB
53 pass_env PATH
54 pass_env /^MYAPP_/
55 </CGI>
56 </Controller::Foo>
57
58=head1 DESCRIPTION
59
c264816e 60Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
61
62Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
63for every invocation. If this is something you need, let me know.
21a20b7e 64
ee75330f 65CGI paths are converted into action names using cgi_action (below.)
66
21a20b7e 67Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
68module for configuration information.
69
70=cut
71
72sub register_actions {
ee75330f 73 my ($self, $app) = @_;
21a20b7e 74
ee75330f 75 my $cgi_bin = $app->path_to('root', 'cgi-bin');
21a20b7e 76
ee75330f 77 my $namespace = $self->action_namespace($app);
21a20b7e 78
79 my $class = ref $self || $self;
80
c264816e 81 for my $file (File::Find::Rule->file->in($cgi_bin)) {
82 my $cgi_path = abs2rel($file, $cgi_bin);
83
84 next if any { $_ eq '.svn' } splitdir $cgi_path;
85
12d29ebf 86 my $path = join '/' => splitdir($cgi_path);
87 my $action_name = $self->cgi_action($path);
9cc2dd4c 88 my $public_path = $self->cgi_path($path);
12d29ebf 89 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
9cc2dd4c 90 my $attrs = { Path => [ $public_path ], Args => [ 0 ] };
12d29ebf 91
21a20b7e 92 my ($cgi, $type);
21a20b7e 93
c264816e 94 if ($self->is_perl_cgi($file)) { # syntax check passed
21a20b7e 95 $type = 'Perl';
12d29ebf 96 $cgi = $self->wrap_perl_cgi($file, $action_name);
21a20b7e 97 } else {
21a20b7e 98 $type = 'Non-Perl';
12d29ebf 99 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
21a20b7e 100 }
101
c264816e 102 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
ee75330f 103 if $app->debug;
21a20b7e 104
c264816e 105 my $code = sub {
21a20b7e 106 my ($controller, $context) = @_;
107 $controller->cgi_to_response($context, $cgi)
108 };
109
110 my $action = $self->create_action(
111 name => $action_name,
112 code => $code,
113 reverse => $reverse,
114 namespace => $namespace,
115 class => $class,
116 attributes => $attrs
117 );
118
ee75330f 119 $app->dispatcher->register($app, $action);
21a20b7e 120 }
121
ee75330f 122 $self->next::method($app, @_);
2340af9d 123
124# Tell Static::Simple to ignore the cgi-bin dir.
125 if (!any{ $_ eq 'cgi-bin' } @{ $app->config->{static}{ignore_dirs}||[] }) {
126 push @{ $app->config->{static}{ignore_dirs} }, 'cgi-bin';
127 }
21a20b7e 128}
129
130=head1 METHODS
131
9cc2dd4c 132=head2 $self->cgi_action($cgi)
21a20b7e 133
134Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
c264816e 135the action name it is registered as. See L</DESCRIPTION> for a discussion on how
136CGI actions are named.
21a20b7e 137
fbaba9dd 138A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
139C<foo/CGI_hlagh__bar_cgi>, for controller Foo, with the C</>s converted to C<__>
140and prepended with C<CGI_>, as well as all non-word characters converted to
141C<_>s. This is because L<Catalyst> action names can't have non-word characters
142in them.
143
144This means that C<foo/bar.cgi> and C<foo__bar.cgi> for example will both map to
145the action C<CGI_foo__bar_cgi> so B<DON'T DO THAT>.
146
21a20b7e 147=cut
148
149sub cgi_action {
150 my ($self, $cgi) = @_;
151
fbaba9dd 152 my $action_name = 'CGI_' . join '__' => split '/' => $cgi;
21a20b7e 153 $action_name =~ s/\W/_/g;
154
155 $action_name
156}
157
9cc2dd4c 158=head2 $self->cgi_path($cgi)
159
160Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
161the public path it should be registered under.
162
163The default is C<cgi-bin/$cgi>.
164
165=cut
166
167sub cgi_path {
168 my ($self, $cgi) = @_;
169 return "cgi-bin/$cgi";
170}
171
c264816e 172=head2 $self->is_perl_cgi($path)
173
174Tries to figure out whether the CGI is Perl or not.
175
176If it's Perl, it will be inlined into a sub instead of being forked off, see
177wrap_perl_cgi (below.)
178
179If it's not doing what you expect, you might want to override it, and let me
180know as well!
181
182=cut
183
184sub is_perl_cgi {
185 my ($self, $cgi) = @_;
186
187 my $shebang = IO::File->new($cgi)->getline;
188
2369e5a4 189 return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
c264816e 190
191 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
192
193 open NULL, '>', File::Spec->devnull;
194 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
195 close NULL;
196 waitpid $pid, 0;
197
198 $? >> 8 == 0
199}
200
12d29ebf 201=head2 $self->wrap_perl_cgi($path, $action_name)
c264816e 202
203Takes the path to a Perl CGI and returns a coderef suitable for passing to
204cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
205
fbaba9dd 206C<$action_name> is the generated name for the action representing the CGI file
207from C<cgi_action>.
c264816e 208
209This is similar to how L<ModPerl::Registry> works, but will only work for
210well-written CGIs. Otherwise, you may have to override this method to do
211something more involved (see L<ModPerl::PerlRun>.)
212
fbaba9dd 213Scripts with C<__DATA__> sections now work too.
214
c264816e 215=cut
216
217sub wrap_perl_cgi {
12d29ebf 218 my ($self, $cgi, $action_name) = @_;
219
1ce18a56 220 my $code = slurp $cgi;
221
fbaba9dd 222 $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms;
1ce18a56 223 my $data = $1;
224
225 my $coderef = do {
12d29ebf 226 no warnings;
bdb35995 227 # catch exit() and turn it into (effectively) a return
228 # we *must* eval STRING because the code needs to be compiled with the
229 # overridden CORE::GLOBAL::exit in view
230 #
231 # set $0 to the name of the cgi file in case it's used there
12d29ebf 232 eval '
bdb35995 233 my $cgi_exited = "EXIT\n";
234 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
235 die [ $cgi_exited, $_[0] || 0 ];
236 } }
12d29ebf 237 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
1ce18a56 238 sub {'
239 . 'local *DATA;'
240 . q{open DATA, '<', \$data;}
bdb35995 241 . qq{local \$0 = "\Q$cgi\E";}
242 . q/my $rv = eval {/
1ce18a56 243 . $code
bdb35995 244 . q/};/
245 . q{
246 return $rv unless $@;
247 die $@ if $@ and not (
248 ref($@) eq 'ARRAY' and
249 $@->[0] eq $cgi_exited
250 );
251 die "exited nonzero: $@->[1]" if $@->[1] != 0;
252 return $rv;
253 }
1ce18a56 254 . '}';
255 };
256
257 croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
258
259 $coderef
c264816e 260}
261
12d29ebf 262=head2 $self->wrap_nonperl_cgi($path, $action_name)
c264816e 263
264Takes the path to a non-Perl CGI and returns a coderef for executing it.
265
12d29ebf 266C<$action_name> is the generated name for the action representing the CGI file.
267
c264816e 268By default returns:
269
270 sub { system $path }
271
272=cut
273
274sub wrap_nonperl_cgi {
12d29ebf 275 my ($self, $cgi, $action_name) = @_;
c264816e 276
277 sub { system $cgi }
278}
279
280=head1 SEE ALSO
281
282L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
283L<Catalyst::Controller>, L<CGI>, L<Catalyst>
284
21a20b7e 285=head1 AUTHOR
286
287Rafael Kitover, C<< <rkitover at cpan.org> >>
288
289=head1 BUGS
290
291Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
292rt.cpan.org>, or through the web interface at
293L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
294I will be notified, and then you'll automatically be notified of progress on
295your bug as I make changes.
296
297=head1 SUPPORT
298
299More information at:
300
301=over 4
302
303=item * RT: CPAN's request tracker
304
305L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
306
307=item * AnnoCPAN: Annotated CPAN documentation
308
309L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
310
311=item * CPAN Ratings
312
313L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
314
315=item * Search CPAN
316
317L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
318
319=back
320
321=head1 COPYRIGHT & LICENSE
322
323Copyright (c) 2008 Rafael Kitover
324
325This program is free software; you can redistribute it and/or modify it
326under the same terms as Perl itself.
327
328=cut
329
3301; # End of Catalyst::Controller::CGIBin
331
332# vim: expandtab shiftwidth=4 ts=4 tw=80: