WrapCGI: implementing mst's suggestions
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
CommitLineData
21a20b7e 1package Catalyst::Controller::CGIBin;
2
3use strict;
4use warnings;
5
6use Class::C3;
7use URI::Escape;
8use File::Slurp 'slurp';
9use File::Find::Rule ();
10use Cwd;
11use Catalyst::Exception ();
c264816e 12use File::Spec::Functions qw/splitdir abs2rel/;
13use IPC::Open3;
14use Symbol 'gensym';
15use List::MoreUtils 'any';
16use IO::File ();
21a20b7e 17
18use parent 'Catalyst::Controller::WrapCGI';
19
20=head1 NAME
21
22Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
23
24=head1 VERSION
25
26Version 0.001
27
28=cut
29
30our $VERSION = '0.001';
31
32=head1 SYNOPSIS
33
34In your controller:
35
36 package MyApp::Controller::Foo;
37
38 use parent qw/Catalyst::Controller::CGIBin/;
39
40 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
41 sub dongs : Local Args(0) {
42 my ($self, $c) = @_;
43 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
44 }
45
46In your .conf:
47
48 <Controller::Foo>
49 <CGI>
50 username_field username # used for REMOTE_USER env var
51 pass_env PERL5LIB
52 pass_env PATH
53 pass_env /^MYAPP_/
54 </CGI>
55 </Controller::Foo>
56
57=head1 DESCRIPTION
58
c264816e 59Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
60
61Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
62for every invocation. If this is something you need, let me know.
21a20b7e 63
ee75330f 64CGI paths are converted into action names using cgi_action (below.)
65
21a20b7e 66A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
67C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
68and prepended with C<CGI_>, as well as all non-word characters converted to
69C<_>s. This is because L<Catalyst> action names can't have non-word characters
70in them.
71
72Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
73module for configuration information.
74
75=cut
76
77sub register_actions {
ee75330f 78 my ($self, $app) = @_;
21a20b7e 79
ee75330f 80 my $cgi_bin = $app->path_to('root', 'cgi-bin');
21a20b7e 81
ee75330f 82 my $namespace = $self->action_namespace($app);
21a20b7e 83
84 my $class = ref $self || $self;
85
c264816e 86 for my $file (File::Find::Rule->file->in($cgi_bin)) {
87 my $cgi_path = abs2rel($file, $cgi_bin);
88
89 next if any { $_ eq '.svn' } splitdir $cgi_path;
90
21a20b7e 91 my ($cgi, $type);
21a20b7e 92
c264816e 93 if ($self->is_perl_cgi($file)) { # syntax check passed
21a20b7e 94 $type = 'Perl';
c264816e 95 $cgi = $self->wrap_perl_cgi($file);
21a20b7e 96 } else {
21a20b7e 97 $type = 'Non-Perl';
c264816e 98 $cgi = $self->wrap_nonperl_cgi($file);
21a20b7e 99 }
100
c264816e 101 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
ee75330f 102 if $app->debug;
21a20b7e 103
c264816e 104 my $path = join '/' => splitdir($cgi_path);
105 my $action_name = $self->cgi_action($path);
21a20b7e 106 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
107 my $attrs = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
108
c264816e 109 my $code = sub {
21a20b7e 110 my ($controller, $context) = @_;
111 $controller->cgi_to_response($context, $cgi)
112 };
113
114 my $action = $self->create_action(
115 name => $action_name,
116 code => $code,
117 reverse => $reverse,
118 namespace => $namespace,
119 class => $class,
120 attributes => $attrs
121 );
122
ee75330f 123 $app->dispatcher->register($app, $action);
21a20b7e 124 }
125
ee75330f 126 $self->next::method($app, @_);
21a20b7e 127}
128
129=head1 METHODS
130
131=head2 $self->cgi_action($cgi_path)
132
133Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
c264816e 134the action name it is registered as. See L</DESCRIPTION> for a discussion on how
135CGI actions are named.
21a20b7e 136
137=cut
138
139sub cgi_action {
140 my ($self, $cgi) = @_;
141
c264816e 142 my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
21a20b7e 143 $action_name =~ s/\W/_/g;
144
145 $action_name
146}
147
c264816e 148=head2 $self->is_perl_cgi($path)
149
150Tries to figure out whether the CGI is Perl or not.
151
152If it's Perl, it will be inlined into a sub instead of being forked off, see
153wrap_perl_cgi (below.)
154
155If it's not doing what you expect, you might want to override it, and let me
156know as well!
157
158=cut
159
160sub is_perl_cgi {
161 my ($self, $cgi) = @_;
162
163 my $shebang = IO::File->new($cgi)->getline;
164
165 return 0 if $shebang !~ /perl/ || $cgi !~ /\.pl\z/;
166
167 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
168
169 open NULL, '>', File::Spec->devnull;
170 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
171 close NULL;
172 waitpid $pid, 0;
173
174 $? >> 8 == 0
175}
176
177=head2 $self->wrap_perl_cgi($path)
178
179Takes the path to a Perl CGI and returns a coderef suitable for passing to
180cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
181
182By default returns:
183
184 eval 'sub {' . slurp($path) . '}'
185
186This is similar to how L<ModPerl::Registry> works, but will only work for
187well-written CGIs. Otherwise, you may have to override this method to do
188something more involved (see L<ModPerl::PerlRun>.)
189
190=cut
191
192sub wrap_perl_cgi {
193 my ($self, $cgi) = @_;
194
195 do { no warnings; eval 'sub {' . slurp($cgi) . '}' }
196}
197
198=head2 $self->wrap_nonperl_cgi($path)
199
200Takes the path to a non-Perl CGI and returns a coderef for executing it.
201
202By default returns:
203
204 sub { system $path }
205
206=cut
207
208sub wrap_nonperl_cgi {
209 my ($self, $cgi) = @_;
210
211 sub { system $cgi }
212}
213
214=head1 SEE ALSO
215
216L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
217L<Catalyst::Controller>, L<CGI>, L<Catalyst>
218
21a20b7e 219=head1 AUTHOR
220
221Rafael Kitover, C<< <rkitover at cpan.org> >>
222
223=head1 BUGS
224
225Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
226rt.cpan.org>, or through the web interface at
227L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
228I will be notified, and then you'll automatically be notified of progress on
229your bug as I make changes.
230
231=head1 SUPPORT
232
233More information at:
234
235=over 4
236
237=item * RT: CPAN's request tracker
238
239L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
240
241=item * AnnoCPAN: Annotated CPAN documentation
242
243L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
244
245=item * CPAN Ratings
246
247L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
248
249=item * Search CPAN
250
251L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
252
253=back
254
255=head1 COPYRIGHT & LICENSE
256
257Copyright (c) 2008 Rafael Kitover
258
259This program is free software; you can redistribute it and/or modify it
260under the same terms as Perl itself.
261
262=cut
263
2641; # End of Catalyst::Controller::CGIBin
265
266# vim: expandtab shiftwidth=4 ts=4 tw=80: