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