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