1 package Catalyst::Controller::WrapCGI;
6 extends 'Catalyst::Controller';
8 use HTTP::Request::AsCGI ();
11 use Catalyst::Exception ();
14 use namespace::clean -except => 'meta';
18 Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
26 our $VERSION = '0.0032';
30 package MyApp::Controller::Foo;
32 use parent qw/Catalyst::Controller::WrapCGI/;
35 sub hello : Path('cgi-bin/hello.cgi') {
38 $self->cgi_to_response($c, sub {
40 print $q->header, $q->start_html('Hello'),
41 $q->h1('Catalyst Rocks!'),
46 In your .conf, configure which environment variables to pass:
50 username_field username # used for REMOTE_USER env var
60 Allows you to run Perl code in a CGI environment derived from your L<Catalyst>
63 B<*WARNING*>: do not export L<CGI> functions into a Controller, it will break
64 with L<Catalyst> 5.8 onward.
66 If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>.
70 C<< $your_controller->{CGI}{pass_env} >> should be an array of environment variables
71 or regular expressions to pass through to your CGIs. Entries surrounded by C</>
72 characters are considered regular expressions.
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
79 Default is to pass the whole of C<%ENV>, except for C<MOD_PERL> (that is, the
80 default C<kill_env> is C<[ 'MOD_PERL' ]>.
82 C<< $your_controller->{CGI}{username_field} >> should be the field for your user's name, which will be
83 read from C<< $c->user->obj >>. Defaults to 'username'.
85 See L</SYNOPSIS> for an example.
89 # Hack-around because Catalyst::Engine::HTTP goes and changes
90 # them to be the remote socket, and FCGI.pm does even dumber things.
92 open my $REAL_STDIN, "<&=".fileno(*STDIN);
93 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
97 =head2 cgi_to_response
99 C<<$self->cgi_to_response($c, $coderef)>>
101 Does the magic of running $coderef in a CGI environment, and populating the
102 appropriate parts of your Catalyst context with the results.
108 sub cgi_to_response {
109 my ($self, $c, $script) = @_;
111 my $res = $self->wrap_cgi($c, $script);
113 # if the CGI doesn't set the response code but sets location they were
114 # probably trying to redirect so set 302 for them
116 my $location = $res->headers->header('Location');
118 if (defined $location && length $location && $res->code == 200) {
119 $c->res->status(302);
121 $c->res->status($res->code);
123 $c->res->body($res->content);
124 $c->res->headers($res->headers);
129 C<<$self->wrap_cgi($c, $coderef)>>
131 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
134 The CGI environment is set up based on $c.
136 The environment variables to pass on are taken from the configuration for your
137 Controller, see L</SYNOPSIS> for an example. If you don't supply a list of
138 environment variables to pass, the whole of %ENV is used.
140 Used by L</cgi_to_response>, which is probably what you want to use as well.
145 my ($self, $c, $call) = @_;
146 my $req = HTTP::Request->new(
147 map { $c->req->$_ } qw/method uri headers/
149 my $body = $c->req->body;
150 my $body_content = '';
152 $req->content_type($c->req->content_type); # set this now so we can override
154 if ($body) { # Slurp from body filehandle
155 local $/; $body_content = <$body>;
157 my $body_params = $c->req->body_parameters;
159 my $encoder = URI->new;
160 $encoder->query_form(%$body_params);
161 $body_content = $encoder->query;
162 $req->content_type('application/x-www-form-urlencoded');
166 my $filtered_env = $self->_filtered_env(\%ENV);
168 $req->content($body_content);
169 $req->content_length(length($body_content));
171 my $username_field = $self->{CGI}{username_field} || 'username';
173 my $username = (($c->can('user_exists') && $c->user_exists)
174 ? eval { $c->user->obj->$username_field }
177 my $path_info = '/'.join '/' => map uri_escape_utf8($_), @{ $c->req->args };
179 my $env = HTTP::Request::AsCGI->new(
181 ($username ? (REMOTE_USER => $username) : ()),
183 PATH_INFO => $path_info,
184 FILEPATH_INFO => '/'.$c->action.$path_info, # eww
185 SCRIPT_NAME => $c->uri_for($c->action)->path
189 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
190 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
192 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
203 Catalyst::Exception->throw(
204 message => "CGI invocation failed: $saved_error"
208 return $env->response;
212 my ($self, $env) = @_;
215 my $pass_env = $self->{CGI}{pass_env};
216 $pass_env = [] if not defined $pass_env;
217 $pass_env = [ $pass_env ] unless ref $pass_env;
219 my $kill_env = $self->{CGI}{kill_env};
220 $kill_env = [ 'MOD_PERL' ] unless defined $kill_env;
221 $kill_env = [ $kill_env ] unless ref $kill_env;
227 push @ok, grep /$re/, keys %$env;
236 for my $k (@$kill_env) {
237 if ($k =~ m!^/(.*)/\z!) {
239 @ok = grep { ! /$re/ } @ok;
241 @ok = grep { $_ ne $k } @ok;
244 return { map {; $_ => $env->{$_} } @ok };
247 __PACKAGE__->meta->make_immutable;
249 =head1 ACKNOWLEDGEMENTS
251 Original development sponsored by L<http://www.altinity.com/>
255 L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>,
256 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
260 Originally written by:
262 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
266 Rafael Kitover C<< <rkitover at cpan.org> >>
268 Hans Dieter Pearcey C<< <hdp at cpan.org> >>
272 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
273 at rt.cpan.org>, or through the web interface at
274 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
275 I will be notified, and then you'll automatically be notified of progress on
276 your bug as I make changes.
284 =item * RT: CPAN's request tracker
286 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
288 =item * AnnoCPAN: Annotated CPAN documentation
290 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
294 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
298 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
302 =head1 COPYRIGHT & LICENSE
304 Copyright (c) 2008 Matt S. Trout
306 This program is free software; you can redistribute it and/or modify it
307 under the same terms as Perl itself.
311 1; # End of Catalyst::Controller::WrapCGI
313 # vim: expandtab shiftwidth=2 ts=2 tw=80: