Commit | Line | Data |
71e6daf6 |
1 | package Catalyst::Controller::WrapCGI; |
b2a17df2 |
2 | |
9cd47364 |
3 | use Moose; |
4 | use mro 'c3'; |
5 | |
6 | extends 'Catalyst::Controller'; |
b2a17df2 |
7 | |
0d83c5de |
8 | use HTTP::Request::AsCGI (); |
9 | use HTTP::Request (); |
10 | use URI (); |
21a20b7e |
11 | use Catalyst::Exception (); |
f410f043 |
12 | use URI::Escape; |
16db0bfc |
13 | use HTTP::Request::Common; |
f410f043 |
14 | |
15 | use namespace::clean -except => 'meta'; |
32b32c62 |
16 | |
17 | =head1 NAME |
18 | |
19 | Catalyst::Controller::WrapCGI - Run CGIs in Catalyst |
20 | |
21 | =head1 VERSION |
22 | |
c0c72ec2 |
23 | Version 0.017 |
32b32c62 |
24 | |
25 | =cut |
26 | |
c0c72ec2 |
27 | our $VERSION = '0.017'; |
32b32c62 |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | package MyApp::Controller::Foo; |
32 | |
33 | use parent qw/Catalyst::Controller::WrapCGI/; |
12d29ebf |
34 | use CGI (); |
32b32c62 |
35 | |
36 | sub hello : Path('cgi-bin/hello.cgi') { |
37 | my ($self, $c) = @_; |
38 | |
39 | $self->cgi_to_response($c, sub { |
12d29ebf |
40 | my $q = CGI->new; |
41 | print $q->header, $q->start_html('Hello'), |
42 | $q->h1('Catalyst Rocks!'), |
43 | $q->end_html; |
32b32c62 |
44 | }); |
45 | } |
46 | |
457c1d76 |
47 | In your .conf, configure which environment variables to pass: |
48 | |
49 | <Controller::Foo> |
50 | <CGI> |
21a20b7e |
51 | username_field username # used for REMOTE_USER env var |
457c1d76 |
52 | pass_env PERL5LIB |
53 | pass_env PATH |
21a20b7e |
54 | pass_env /^MYAPP_/ |
c4ef0be5 |
55 | kill_env MYAPP_BAD |
457c1d76 |
56 | </CGI> |
57 | </Controller::Foo> |
58 | |
59 | =head1 DESCRIPTION |
60 | |
61 | Allows you to run Perl code in a CGI environment derived from your L<Catalyst> |
62 | context. |
63 | |
12d29ebf |
64 | B<*WARNING*>: do not export L<CGI> functions into a Controller, it will break |
65 | with L<Catalyst> 5.8 onward. |
66 | |
21a20b7e |
67 | If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>. |
68 | |
b9548267 |
69 | C<REMOTE_USER> will be set to C<< $c->user->obj->$username_field >> if |
70 | available, or to C<< $c->req->remote_user >> otherwise. |
71 | |
21a20b7e |
72 | =head1 CONFIGURATION |
73 | |
16bed4c4 |
74 | =head2 pass_env |
75 | |
c212b57b |
76 | C<< $your_controller->{CGI}{pass_env} >> should be an array of environment variables |
21a20b7e |
77 | or regular expressions to pass through to your CGIs. Entries surrounded by C</> |
78 | characters are considered regular expressions. |
79 | |
16bed4c4 |
80 | =head2 kill_env |
81 | |
c212b57b |
82 | C<< $your_controller->{CGI}{kill_env} >> should be an array of environment |
83 | variables or regular expressions to remove from the environment before passing |
84 | it to your CGIs. Entries surrounded by C</> characters are considered regular |
85 | expressions. |
21a20b7e |
86 | |
16bed4c4 |
87 | Default is to pass the whole of C<%ENV>, except for entries listed in |
88 | L</FILTERED ENVIRONMENT> below. |
c212b57b |
89 | |
16bed4c4 |
90 | =head2 username_field |
91 | |
92 | C<< $your_controller->{CGI}{username_field} >> should be the field for your |
93 | user's name, which will be read from C<< $c->user->obj >>. Defaults to |
94 | 'username'. |
21a20b7e |
95 | |
96 | See L</SYNOPSIS> for an example. |
97 | |
32b32c62 |
98 | =cut |
b2a17df2 |
99 | |
100 | # Hack-around because Catalyst::Engine::HTTP goes and changes |
101 | # them to be the remote socket, and FCGI.pm does even dumber things. |
102 | |
32b32c62 |
103 | open my $REAL_STDIN, "<&=".fileno(*STDIN); |
104 | open my $REAL_STDOUT, ">>&=".fileno(*STDOUT); |
105 | |
106 | =head1 METHODS |
107 | |
f410f043 |
108 | =head2 cgi_to_response |
109 | |
b9548267 |
110 | C<< $self->cgi_to_response($c, $coderef) >> |
32b32c62 |
111 | |
112 | Does the magic of running $coderef in a CGI environment, and populating the |
113 | appropriate parts of your Catalyst context with the results. |
114 | |
f410f043 |
115 | Calls L</wrap_cgi>. |
457c1d76 |
116 | |
32b32c62 |
117 | =cut |
b2a17df2 |
118 | |
119 | sub cgi_to_response { |
120 | my ($self, $c, $script) = @_; |
0d83c5de |
121 | |
b2a17df2 |
122 | my $res = $self->wrap_cgi($c, $script); |
123 | |
124 | # if the CGI doesn't set the response code but sets location they were |
125 | # probably trying to redirect so set 302 for them |
126 | |
32b32c62 |
127 | my $location = $res->headers->header('Location'); |
128 | |
129 | if (defined $location && length $location && $res->code == 200) { |
b2a17df2 |
130 | $c->res->status(302); |
131 | } else { |
132 | $c->res->status($res->code); |
133 | } |
134 | $c->res->body($res->content); |
135 | $c->res->headers($res->headers); |
136 | } |
137 | |
f410f043 |
138 | =head2 wrap_cgi |
139 | |
b9548267 |
140 | C<< $self->wrap_cgi($c, $coderef) >> |
32b32c62 |
141 | |
142 | Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an |
143 | L<HTTP::Response>. |
144 | |
b9548267 |
145 | The CGI environment is set up based on C<$c>. |
32b32c62 |
146 | |
457c1d76 |
147 | The environment variables to pass on are taken from the configuration for your |
148 | Controller, see L</SYNOPSIS> for an example. If you don't supply a list of |
b9548267 |
149 | environment variables to pass, the whole of %ENV is used (with exceptions listed |
150 | in L</FILTERED ENVIRONMENT>. |
457c1d76 |
151 | |
f410f043 |
152 | Used by L</cgi_to_response>, which is probably what you want to use as well. |
32b32c62 |
153 | |
154 | =cut |
155 | |
b2a17df2 |
156 | sub wrap_cgi { |
157 | my ($self, $c, $call) = @_; |
158 | my $req = HTTP::Request->new( |
159 | map { $c->req->$_ } qw/method uri headers/ |
160 | ); |
161 | my $body = $c->req->body; |
162 | my $body_content = ''; |
163 | |
164 | $req->content_type($c->req->content_type); # set this now so we can override |
165 | |
166 | if ($body) { # Slurp from body filehandle |
167 | local $/; $body_content = <$body>; |
168 | } else { |
169 | my $body_params = $c->req->body_parameters; |
16db0bfc |
170 | |
171 | if (my %uploads = %{ $c->req->uploads }) { |
172 | my $post = POST 'http://localhost/', |
173 | Content_Type => 'form-data', |
174 | Content => [ |
175 | %$body_params, |
176 | map { |
177 | my $upl = $uploads{$_}; |
178 | $_ => [ |
179 | undef, |
180 | $upl->filename, |
181 | Content => $upl->slurp, |
01c35d4e |
182 | map { |
183 | my $header = $_; |
184 | map { $header => $_ } $upl->headers->header($header) |
185 | } $upl->headers->header_field_names |
16db0bfc |
186 | ] |
187 | } keys %uploads |
188 | ]; |
189 | $body_content = $post->content; |
190 | $req->content_type($post->header('Content-Type')); |
191 | } elsif (%$body_params) { |
32b32c62 |
192 | my $encoder = URI->new; |
193 | $encoder->query_form(%$body_params); |
194 | $body_content = $encoder->query; |
b2a17df2 |
195 | $req->content_type('application/x-www-form-urlencoded'); |
196 | } |
197 | } |
198 | |
c212b57b |
199 | my $filtered_env = $self->_filtered_env(\%ENV); |
457c1d76 |
200 | |
b2a17df2 |
201 | $req->content($body_content); |
202 | $req->content_length(length($body_content)); |
21a20b7e |
203 | |
204 | my $username_field = $self->{CGI}{username_field} || 'username'; |
205 | |
206 | my $username = (($c->can('user_exists') && $c->user_exists) |
207 | ? eval { $c->user->obj->$username_field } |
b2a17df2 |
208 | : ''); |
0d83c5de |
209 | |
b9548267 |
210 | $username ||= $c->req->remote_user if $c->req->can('remote_user'); |
211 | |
83bba9ab |
212 | my $path_info = '/'.join '/' => map { |
213 | utf8::is_utf8($_) ? uri_escape_utf8($_) : uri_escape($_) |
214 | } @{ $c->req->args }; |
0d83c5de |
215 | |
b2a17df2 |
216 | my $env = HTTP::Request::AsCGI->new( |
217 | $req, |
21a20b7e |
218 | ($username ? (REMOTE_USER => $username) : ()), |
c212b57b |
219 | %$filtered_env, |
f410f043 |
220 | PATH_INFO => $path_info, |
c4ef0be5 |
221 | # eww, this is likely broken: |
222 | FILEPATH_INFO => '/'.$c->action.$path_info, |
17a050f8 |
223 | SCRIPT_NAME => $c->uri_for($c->action, $c->req->captures)->path |
b2a17df2 |
224 | ); |
225 | |
226 | { |
32b32c62 |
227 | local *STDIN = $REAL_STDIN; # restore the real ones so the filenos |
228 | local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup |
b2a17df2 |
229 | |
32b32c62 |
230 | my $old = select($REAL_STDOUT); # in case somebody just calls 'print' |
b2a17df2 |
231 | |
232 | my $saved_error; |
233 | |
234 | $env->setup; |
235 | eval { $call->() }; |
236 | $saved_error = $@; |
237 | $env->restore; |
238 | |
239 | select($old); |
240 | |
21a20b7e |
241 | Catalyst::Exception->throw( |
242 | message => "CGI invocation failed: $saved_error" |
243 | ) if $saved_error; |
b2a17df2 |
244 | } |
245 | |
246 | return $env->response; |
247 | } |
248 | |
16bed4c4 |
249 | =head1 FILTERED ENVIRONMENT |
250 | |
251 | If you don't use the L</pass_env> option to restrict which environment variables |
252 | are passed in, the default is to pass the whole of C<%ENV> except the variables |
253 | listed below. |
254 | |
255 | MOD_PERL |
256 | SERVER_SOFTWARE |
257 | SERVER_NAME |
258 | GATEWAY_INTERFACE |
259 | SERVER_PROTOCOL |
260 | SERVER_PORT |
261 | REQUEST_METHOD |
262 | PATH_INFO |
263 | PATH_TRANSLATED |
264 | SCRIPT_NAME |
265 | QUERY_STRING |
266 | REMOTE_HOST |
267 | REMOTE_ADDR |
268 | AUTH_TYPE |
269 | REMOTE_USER |
270 | REMOTE_IDENT |
271 | CONTENT_TYPE |
272 | CONTENT_LENGTH |
273 | HTTP_ACCEPT |
274 | HTTP_USER_AGENT |
275 | |
276 | C<%ENV> can be further trimmed using L</kill_env>. |
277 | |
278 | =cut |
279 | |
280 | my $DEFAULT_KILL_ENV = [qw/ |
281 | MOD_PERL SERVER_SOFTWARE SERVER_NAME GATEWAY_INTERFACE SERVER_PROTOCOL |
282 | SERVER_PORT REQUEST_METHOD PATH_INFO PATH_TRANSLATED SCRIPT_NAME QUERY_STRING |
283 | REMOTE_HOST REMOTE_ADDR AUTH_TYPE REMOTE_USER REMOTE_IDENT CONTENT_TYPE |
284 | CONTENT_LENGTH HTTP_ACCEPT HTTP_USER_AGENT |
285 | /]; |
286 | |
0d83c5de |
287 | sub _filtered_env { |
288 | my ($self, $env) = @_; |
289 | my @ok; |
290 | |
291 | my $pass_env = $self->{CGI}{pass_env}; |
292 | $pass_env = [] if not defined $pass_env; |
293 | $pass_env = [ $pass_env ] unless ref $pass_env; |
294 | |
295 | my $kill_env = $self->{CGI}{kill_env}; |
16bed4c4 |
296 | $kill_env = $DEFAULT_KILL_ENV unless defined $kill_env; |
0d83c5de |
297 | $kill_env = [ $kill_env ] unless ref $kill_env; |
298 | |
299 | if (@$pass_env) { |
300 | for (@$pass_env) { |
301 | if (m!^/(.*)/\z!) { |
302 | my $re = qr/$1/; |
303 | push @ok, grep /$re/, keys %$env; |
304 | } else { |
305 | push @ok, $_; |
306 | } |
307 | } |
308 | } else { |
309 | @ok = keys %$env; |
310 | } |
311 | |
312 | for my $k (@$kill_env) { |
313 | if ($k =~ m!^/(.*)/\z!) { |
314 | my $re = qr/$1/; |
315 | @ok = grep { ! /$re/ } @ok; |
316 | } else { |
317 | @ok = grep { $_ ne $k } @ok; |
318 | } |
319 | } |
320 | return { map {; $_ => $env->{$_} } @ok }; |
321 | } |
322 | |
f410f043 |
323 | __PACKAGE__->meta->make_immutable; |
0d83c5de |
324 | |
c4ef0be5 |
325 | =head1 DIRECT SOCKET/NPH SCRIPTS |
326 | |
327 | This currently won't work: |
328 | |
329 | #!/usr/bin/perl |
330 | |
331 | use CGI ':standard'; |
332 | |
333 | $| = 1; |
334 | |
335 | print header; |
336 | |
337 | for (0..1000) { |
338 | print $_, br, "\n"; |
83bba9ab |
339 | sleep 1; |
c4ef0be5 |
340 | } |
341 | |
342 | because the coderef is executed synchronously with C<STDOUT> pointing to a temp |
343 | file. |
344 | |
32b32c62 |
345 | =head1 ACKNOWLEDGEMENTS |
346 | |
347 | Original development sponsored by L<http://www.altinity.com/> |
348 | |
457c1d76 |
349 | =head1 SEE ALSO |
350 | |
21a20b7e |
351 | L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>, |
457c1d76 |
352 | L<Catalyst::Controller>, L<CGI>, L<Catalyst> |
353 | |
0d83c5de |
354 | =head1 AUTHORS |
355 | |
356 | Originally written by: |
32b32c62 |
357 | |
358 | Matt S. Trout, C<< <mst at shadowcat.co.uk> >> |
359 | |
0d83c5de |
360 | Contributors: |
361 | |
362 | Rafael Kitover C<< <rkitover at cpan.org> >> |
363 | |
364 | Hans Dieter Pearcey C<< <hdp at cpan.org> >> |
365 | |
32b32c62 |
366 | =head1 BUGS |
367 | |
368 | Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi |
369 | at rt.cpan.org>, or through the web interface at |
370 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>. |
371 | I will be notified, and then you'll automatically be notified of progress on |
372 | your bug as I make changes. |
373 | |
374 | =head1 SUPPORT |
375 | |
376 | More information at: |
377 | |
378 | =over 4 |
379 | |
380 | =item * RT: CPAN's request tracker |
381 | |
382 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI> |
383 | |
384 | =item * AnnoCPAN: Annotated CPAN documentation |
385 | |
386 | L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI> |
387 | |
388 | =item * CPAN Ratings |
389 | |
390 | L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI> |
391 | |
392 | =item * Search CPAN |
393 | |
394 | L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI> |
395 | |
396 | =back |
397 | |
398 | =head1 COPYRIGHT & LICENSE |
399 | |
400 | Copyright (c) 2008 Matt S. Trout |
401 | |
402 | This program is free software; you can redistribute it and/or modify it |
403 | under the same terms as Perl itself. |
404 | |
405 | =cut |
406 | |
407 | 1; # End of Catalyst::Controller::WrapCGI |
408 | |
21a20b7e |
409 | # vim: expandtab shiftwidth=2 ts=2 tw=80: |