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; |
13 | |
14 | use namespace::clean -except => 'meta'; |
32b32c62 |
15 | |
16 | =head1 NAME |
17 | |
18 | Catalyst::Controller::WrapCGI - Run CGIs in Catalyst |
19 | |
20 | =head1 VERSION |
21 | |
8ccf44c7 |
22 | Version 0.0032 |
32b32c62 |
23 | |
24 | =cut |
25 | |
8ccf44c7 |
26 | our $VERSION = '0.0032'; |
32b32c62 |
27 | |
28 | =head1 SYNOPSIS |
29 | |
30 | package MyApp::Controller::Foo; |
31 | |
32 | use parent qw/Catalyst::Controller::WrapCGI/; |
12d29ebf |
33 | use CGI (); |
32b32c62 |
34 | |
35 | sub hello : Path('cgi-bin/hello.cgi') { |
36 | my ($self, $c) = @_; |
37 | |
38 | $self->cgi_to_response($c, sub { |
12d29ebf |
39 | my $q = CGI->new; |
40 | print $q->header, $q->start_html('Hello'), |
41 | $q->h1('Catalyst Rocks!'), |
42 | $q->end_html; |
32b32c62 |
43 | }); |
44 | } |
45 | |
457c1d76 |
46 | In your .conf, configure which environment variables to pass: |
47 | |
48 | <Controller::Foo> |
49 | <CGI> |
21a20b7e |
50 | username_field username # used for REMOTE_USER env var |
457c1d76 |
51 | pass_env PERL5LIB |
52 | pass_env PATH |
21a20b7e |
53 | pass_env /^MYAPP_/ |
c212b57b |
54 | kill_env MOD_PERL |
457c1d76 |
55 | </CGI> |
56 | </Controller::Foo> |
57 | |
58 | =head1 DESCRIPTION |
59 | |
60 | Allows you to run Perl code in a CGI environment derived from your L<Catalyst> |
61 | context. |
62 | |
12d29ebf |
63 | B<*WARNING*>: do not export L<CGI> functions into a Controller, it will break |
64 | with L<Catalyst> 5.8 onward. |
65 | |
21a20b7e |
66 | If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>. |
67 | |
68 | =head1 CONFIGURATION |
69 | |
c212b57b |
70 | C<< $your_controller->{CGI}{pass_env} >> should be an array of environment variables |
21a20b7e |
71 | or regular expressions to pass through to your CGIs. Entries surrounded by C</> |
72 | characters are considered regular expressions. |
73 | |
c212b57b |
74 | C<< $your_controller->{CGI}{kill_env} >> should be an array of environment |
75 | variables or regular expressions to remove from the environment before passing |
76 | it to your CGIs. Entries surrounded by C</> characters are considered regular |
77 | expressions. |
21a20b7e |
78 | |
a032b7f8 |
79 | Default is to pass the whole of C<%ENV>, except for C<MOD_PERL> and |
80 | C<CONTENT_TYPE> (that is, the default C<kill_env> is C<[ qw(MOD_PERL |
81 | CONTENT_TYPE) ]>. |
c212b57b |
82 | |
83 | C<< $your_controller->{CGI}{username_field} >> should be the field for your user's name, which will be |
84 | read from C<< $c->user->obj >>. Defaults to 'username'. |
21a20b7e |
85 | |
86 | See L</SYNOPSIS> for an example. |
87 | |
32b32c62 |
88 | =cut |
b2a17df2 |
89 | |
90 | # Hack-around because Catalyst::Engine::HTTP goes and changes |
91 | # them to be the remote socket, and FCGI.pm does even dumber things. |
92 | |
32b32c62 |
93 | open my $REAL_STDIN, "<&=".fileno(*STDIN); |
94 | open my $REAL_STDOUT, ">>&=".fileno(*STDOUT); |
95 | |
96 | =head1 METHODS |
97 | |
f410f043 |
98 | =head2 cgi_to_response |
99 | |
100 | C<<$self->cgi_to_response($c, $coderef)>> |
32b32c62 |
101 | |
102 | Does the magic of running $coderef in a CGI environment, and populating the |
103 | appropriate parts of your Catalyst context with the results. |
104 | |
f410f043 |
105 | Calls L</wrap_cgi>. |
457c1d76 |
106 | |
32b32c62 |
107 | =cut |
b2a17df2 |
108 | |
109 | sub cgi_to_response { |
110 | my ($self, $c, $script) = @_; |
0d83c5de |
111 | |
b2a17df2 |
112 | my $res = $self->wrap_cgi($c, $script); |
113 | |
114 | # if the CGI doesn't set the response code but sets location they were |
115 | # probably trying to redirect so set 302 for them |
116 | |
32b32c62 |
117 | my $location = $res->headers->header('Location'); |
118 | |
119 | if (defined $location && length $location && $res->code == 200) { |
b2a17df2 |
120 | $c->res->status(302); |
121 | } else { |
122 | $c->res->status($res->code); |
123 | } |
124 | $c->res->body($res->content); |
125 | $c->res->headers($res->headers); |
126 | } |
127 | |
f410f043 |
128 | =head2 wrap_cgi |
129 | |
130 | C<<$self->wrap_cgi($c, $coderef)>> |
32b32c62 |
131 | |
132 | Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an |
133 | L<HTTP::Response>. |
134 | |
135 | The CGI environment is set up based on $c. |
136 | |
457c1d76 |
137 | The environment variables to pass on are taken from the configuration for your |
138 | Controller, see L</SYNOPSIS> for an example. If you don't supply a list of |
139 | environment variables to pass, the whole of %ENV is used. |
140 | |
f410f043 |
141 | Used by L</cgi_to_response>, which is probably what you want to use as well. |
32b32c62 |
142 | |
143 | =cut |
144 | |
b2a17df2 |
145 | sub wrap_cgi { |
146 | my ($self, $c, $call) = @_; |
147 | my $req = HTTP::Request->new( |
148 | map { $c->req->$_ } qw/method uri headers/ |
149 | ); |
150 | my $body = $c->req->body; |
151 | my $body_content = ''; |
152 | |
153 | $req->content_type($c->req->content_type); # set this now so we can override |
154 | |
155 | if ($body) { # Slurp from body filehandle |
156 | local $/; $body_content = <$body>; |
157 | } else { |
158 | my $body_params = $c->req->body_parameters; |
32b32c62 |
159 | if (%$body_params) { |
160 | my $encoder = URI->new; |
161 | $encoder->query_form(%$body_params); |
162 | $body_content = $encoder->query; |
b2a17df2 |
163 | $req->content_type('application/x-www-form-urlencoded'); |
164 | } |
165 | } |
166 | |
c212b57b |
167 | my $filtered_env = $self->_filtered_env(\%ENV); |
457c1d76 |
168 | |
b2a17df2 |
169 | $req->content($body_content); |
170 | $req->content_length(length($body_content)); |
21a20b7e |
171 | |
172 | my $username_field = $self->{CGI}{username_field} || 'username'; |
173 | |
174 | my $username = (($c->can('user_exists') && $c->user_exists) |
175 | ? eval { $c->user->obj->$username_field } |
b2a17df2 |
176 | : ''); |
0d83c5de |
177 | |
f410f043 |
178 | my $path_info = '/'.join '/' => map uri_escape_utf8($_), @{ $c->req->args }; |
0d83c5de |
179 | |
b2a17df2 |
180 | my $env = HTTP::Request::AsCGI->new( |
181 | $req, |
21a20b7e |
182 | ($username ? (REMOTE_USER => $username) : ()), |
c212b57b |
183 | %$filtered_env, |
f410f043 |
184 | PATH_INFO => $path_info, |
185 | FILEPATH_INFO => '/'.$c->action.$path_info, # eww |
186 | SCRIPT_NAME => $c->uri_for($c->action)->path |
b2a17df2 |
187 | ); |
188 | |
189 | { |
32b32c62 |
190 | local *STDIN = $REAL_STDIN; # restore the real ones so the filenos |
191 | local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup |
b2a17df2 |
192 | |
32b32c62 |
193 | my $old = select($REAL_STDOUT); # in case somebody just calls 'print' |
b2a17df2 |
194 | |
195 | my $saved_error; |
196 | |
197 | $env->setup; |
198 | eval { $call->() }; |
199 | $saved_error = $@; |
200 | $env->restore; |
201 | |
202 | select($old); |
203 | |
21a20b7e |
204 | Catalyst::Exception->throw( |
205 | message => "CGI invocation failed: $saved_error" |
206 | ) if $saved_error; |
b2a17df2 |
207 | } |
208 | |
209 | return $env->response; |
210 | } |
211 | |
0d83c5de |
212 | sub _filtered_env { |
213 | my ($self, $env) = @_; |
214 | my @ok; |
215 | |
216 | my $pass_env = $self->{CGI}{pass_env}; |
217 | $pass_env = [] if not defined $pass_env; |
218 | $pass_env = [ $pass_env ] unless ref $pass_env; |
219 | |
220 | my $kill_env = $self->{CGI}{kill_env}; |
a032b7f8 |
221 | $kill_env = [ 'MOD_PERL', 'CONTENT_TYPE' ] unless defined $kill_env; |
0d83c5de |
222 | $kill_env = [ $kill_env ] unless ref $kill_env; |
223 | |
224 | if (@$pass_env) { |
225 | for (@$pass_env) { |
226 | if (m!^/(.*)/\z!) { |
227 | my $re = qr/$1/; |
228 | push @ok, grep /$re/, keys %$env; |
229 | } else { |
230 | push @ok, $_; |
231 | } |
232 | } |
233 | } else { |
234 | @ok = keys %$env; |
235 | } |
236 | |
237 | for my $k (@$kill_env) { |
238 | if ($k =~ m!^/(.*)/\z!) { |
239 | my $re = qr/$1/; |
240 | @ok = grep { ! /$re/ } @ok; |
241 | } else { |
242 | @ok = grep { $_ ne $k } @ok; |
243 | } |
244 | } |
245 | return { map {; $_ => $env->{$_} } @ok }; |
246 | } |
247 | |
f410f043 |
248 | __PACKAGE__->meta->make_immutable; |
0d83c5de |
249 | |
32b32c62 |
250 | =head1 ACKNOWLEDGEMENTS |
251 | |
252 | Original development sponsored by L<http://www.altinity.com/> |
253 | |
457c1d76 |
254 | =head1 SEE ALSO |
255 | |
21a20b7e |
256 | L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>, |
457c1d76 |
257 | L<Catalyst::Controller>, L<CGI>, L<Catalyst> |
258 | |
0d83c5de |
259 | =head1 AUTHORS |
260 | |
261 | Originally written by: |
32b32c62 |
262 | |
263 | Matt S. Trout, C<< <mst at shadowcat.co.uk> >> |
264 | |
0d83c5de |
265 | Contributors: |
266 | |
267 | Rafael Kitover C<< <rkitover at cpan.org> >> |
268 | |
269 | Hans Dieter Pearcey C<< <hdp at cpan.org> >> |
270 | |
32b32c62 |
271 | =head1 BUGS |
272 | |
273 | Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi |
274 | at rt.cpan.org>, or through the web interface at |
275 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>. |
276 | I will be notified, and then you'll automatically be notified of progress on |
277 | your bug as I make changes. |
278 | |
279 | =head1 SUPPORT |
280 | |
281 | More information at: |
282 | |
283 | =over 4 |
284 | |
285 | =item * RT: CPAN's request tracker |
286 | |
287 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI> |
288 | |
289 | =item * AnnoCPAN: Annotated CPAN documentation |
290 | |
291 | L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI> |
292 | |
293 | =item * CPAN Ratings |
294 | |
295 | L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI> |
296 | |
297 | =item * Search CPAN |
298 | |
299 | L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI> |
300 | |
301 | =back |
302 | |
303 | =head1 COPYRIGHT & LICENSE |
304 | |
305 | Copyright (c) 2008 Matt S. Trout |
306 | |
307 | This program is free software; you can redistribute it and/or modify it |
308 | under the same terms as Perl itself. |
309 | |
310 | =cut |
311 | |
312 | 1; # End of Catalyst::Controller::WrapCGI |
313 | |
21a20b7e |
314 | # vim: expandtab shiftwidth=2 ts=2 tw=80: |