1 package Catalyst::Controller::WrapCGI;
6 extends 'Catalyst::Controller';
8 use HTTP::Request::AsCGI;
11 use Catalyst::Exception ();
15 Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
23 our $VERSION = '0.0029';
27 package MyApp::Controller::Foo;
29 use parent qw/Catalyst::Controller::WrapCGI/;
32 sub hello : Path('cgi-bin/hello.cgi') {
35 $self->cgi_to_response($c, sub {
37 print $q->header, $q->start_html('Hello'),
38 $q->h1('Catalyst Rocks!'),
43 In your .conf, configure which environment variables to pass:
47 username_field username # used for REMOTE_USER env var
57 Allows you to run Perl code in a CGI environment derived from your L<Catalyst>
60 B<*WARNING*>: do not export L<CGI> functions into a Controller, it will break
61 with L<Catalyst> 5.8 onward.
63 If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>.
67 C<< $your_controller->{CGI}{pass_env} >> should be an array of environment variables
68 or regular expressions to pass through to your CGIs. Entries surrounded by C</>
69 characters are considered regular expressions.
71 C<< $your_controller->{CGI}{kill_env} >> should be an array of environment
72 variables or regular expressions to remove from the environment before passing
73 it to your CGIs. Entries surrounded by C</> characters are considered regular
76 Default is to pass the whole of C<%ENV>, except for C<MOD_PERL> (that is, the
77 default C<kill_env> is C<[ 'MOD_PERL' ]>.
79 C<< $your_controller->{CGI}{username_field} >> should be the field for your user's name, which will be
80 read from C<< $c->user->obj >>. Defaults to 'username'.
82 See L</SYNOPSIS> for an example.
86 # Hack-around because Catalyst::Engine::HTTP goes and changes
87 # them to be the remote socket, and FCGI.pm does even dumber things.
89 open my $REAL_STDIN, "<&=".fileno(*STDIN);
90 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
94 =head2 $self->cgi_to_response($c, $coderef)
96 Does the magic of running $coderef in a CGI environment, and populating the
97 appropriate parts of your Catalyst context with the results.
99 Calls wrap_cgi (below.)
103 sub cgi_to_response {
104 my ($self, $c, $script) = @_;
105 my $res = $self->wrap_cgi($c, $script);
107 # if the CGI doesn't set the response code but sets location they were
108 # probably trying to redirect so set 302 for them
110 my $location = $res->headers->header('Location');
112 if (defined $location && length $location && $res->code == 200) {
113 $c->res->status(302);
115 $c->res->status($res->code);
117 $c->res->body($res->content);
118 $c->res->headers($res->headers);
121 =head2 $self->wrap_cgi($c, $coderef)
123 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
126 The CGI environment is set up based on $c.
128 The environment variables to pass on are taken from the configuration for your
129 Controller, see L</SYNOPSIS> for an example. If you don't supply a list of
130 environment variables to pass, the whole of %ENV is used.
132 Used by cgi_to_response (above), which is probably what you want to use as well.
137 my ($self, $env) = @_;
140 my $pass_env = $self->{CGI}{pass_env};
141 $pass_env = [] if not defined $pass_env;
142 $pass_env = [ $pass_env ] unless ref $pass_env;
144 my $kill_env = $self->{CGI}{kill_env};
145 $kill_env = [ 'MOD_PERL' ] unless defined $kill_env;
146 $kill_env = [ $kill_env ] unless ref $kill_env;
152 push @ok, grep /$re/, keys %$env;
161 for my $k (@$kill_env) {
162 if ($k =~ m!^/(.*)/\z!) {
164 @ok = grep { ! /$re/ } @ok;
166 @ok = grep { $_ ne $k } @ok;
169 return { map {; $_ => $env->{$_} } @ok };
173 my ($self, $c, $call) = @_;
174 my $req = HTTP::Request->new(
175 map { $c->req->$_ } qw/method uri headers/
177 my $body = $c->req->body;
178 my $body_content = '';
180 $req->content_type($c->req->content_type); # set this now so we can override
182 if ($body) { # Slurp from body filehandle
183 local $/; $body_content = <$body>;
185 my $body_params = $c->req->body_parameters;
187 my $encoder = URI->new;
188 $encoder->query_form(%$body_params);
189 $body_content = $encoder->query;
190 $req->content_type('application/x-www-form-urlencoded');
194 my $filtered_env = $self->_filtered_env(\%ENV);
196 $req->content($body_content);
197 $req->content_length(length($body_content));
199 my $username_field = $self->{CGI}{username_field} || 'username';
201 my $username = (($c->can('user_exists') && $c->user_exists)
202 ? eval { $c->user->obj->$username_field }
204 my $env = HTTP::Request::AsCGI->new(
206 ($username ? (REMOTE_USER => $username) : ()),
211 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
212 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
214 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
225 Catalyst::Exception->throw(
226 message => "CGI invocation failed: $saved_error"
230 return $env->response;
233 =head1 ACKNOWLEDGEMENTS
235 Original development sponsored by L<http://www.altinity.com/>
239 L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>,
240 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
244 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
248 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
249 at rt.cpan.org>, or through the web interface at
250 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
251 I will be notified, and then you'll automatically be notified of progress on
252 your bug as I make changes.
260 =item * RT: CPAN's request tracker
262 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
264 =item * AnnoCPAN: Annotated CPAN documentation
266 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
270 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
274 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
278 =head1 COPYRIGHT & LICENSE
280 Copyright (c) 2008 Matt S. Trout
282 This program is free software; you can redistribute it and/or modify it
283 under the same terms as Perl itself.
287 1; # End of Catalyst::Controller::WrapCGI
289 # vim: expandtab shiftwidth=2 ts=2 tw=80: