1 package Catalyst::Controller::WrapCGI;
5 use parent 'Catalyst::Controller';
7 use HTTP::Request::AsCGI;
10 use Catalyst::Exception ();
14 Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
22 our $VERSION = '0.0026';
26 package MyApp::Controller::Foo;
28 use parent qw/Catalyst::Controller::WrapCGI/;
31 sub hello : Path('cgi-bin/hello.cgi') {
34 $self->cgi_to_response($c, sub {
36 print $q->header, $q->start_html('Hello'),
37 $q->h1('Catalyst Rocks!'),
42 In your .conf, configure which environment variables to pass:
46 username_field username # used for REMOTE_USER env var
55 Allows you to run Perl code in a CGI environment derived from your L<Catalyst>
58 B<*WARNING*>: do not export L<CGI> functions into a Controller, it will break
59 with L<Catalyst> 5.8 onward.
61 If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>.
65 C<$your_controller->{CGI}{pass_env}> should be an array of environment variables
66 or regular expressions to pass through to your CGIs. Entries surrounded by C</>
67 characters are considered regular expressions.
69 Default is to pass the whole of C<%ENV>.
71 C<{CGI}{username_field}> should be the field for your user's name, which will be
72 read from C<$c->user->obj>. Defaults to 'username'.
74 See L</SYNOPSIS> for an example.
78 # Hack-around because Catalyst::Engine::HTTP goes and changes
79 # them to be the remote socket, and FCGI.pm does even dumber things.
81 open my $REAL_STDIN, "<&=".fileno(*STDIN);
82 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
86 =head2 $self->cgi_to_response($c, $coderef)
88 Does the magic of running $coderef in a CGI environment, and populating the
89 appropriate parts of your Catalyst context with the results.
91 Calls wrap_cgi (below.)
96 my ($self, $c, $script) = @_;
97 my $res = $self->wrap_cgi($c, $script);
99 # if the CGI doesn't set the response code but sets location they were
100 # probably trying to redirect so set 302 for them
102 my $location = $res->headers->header('Location');
104 if (defined $location && length $location && $res->code == 200) {
105 $c->res->status(302);
107 $c->res->status($res->code);
109 $c->res->body($res->content);
110 $c->res->headers($res->headers);
113 =head2 $self->wrap_cgi($c, $coderef)
115 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
118 The CGI environment is set up based on $c.
120 The environment variables to pass on are taken from the configuration for your
121 Controller, see L</SYNOPSIS> for an example. If you don't supply a list of
122 environment variables to pass, the whole of %ENV is used.
124 Used by cgi_to_response (above), which is probably what you want to use as well.
129 my ($self, $c, $call) = @_;
130 my $req = HTTP::Request->new(
131 map { $c->req->$_ } qw/method uri headers/
133 my $body = $c->req->body;
134 my $body_content = '';
136 $req->content_type($c->req->content_type); # set this now so we can override
138 if ($body) { # Slurp from body filehandle
139 local $/; $body_content = <$body>;
141 my $body_params = $c->req->body_parameters;
143 my $encoder = URI->new;
144 $encoder->query_form(%$body_params);
145 $body_content = $encoder->query;
146 $req->content_type('application/x-www-form-urlencoded');
152 my $pass_env = $self->{CGI}{pass_env};
153 $pass_env = [] if not defined $pass_env;
154 $pass_env = [ $pass_env ] unless ref $pass_env;
159 push @env, grep /$re/, keys %ENV;
165 @env = keys %ENV unless @env;
167 $req->content($body_content);
168 $req->content_length(length($body_content));
170 my $username_field = $self->{CGI}{username_field} || 'username';
172 my $username = (($c->can('user_exists') && $c->user_exists)
173 ? eval { $c->user->obj->$username_field }
175 my $env = HTTP::Request::AsCGI->new(
177 ($username ? (REMOTE_USER => $username) : ()),
178 map { ($_, $ENV{$_}) } @env
182 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
183 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
185 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
196 Catalyst::Exception->throw(
197 message => "CGI invocation failed: $saved_error"
201 return $env->response;
204 =head1 ACKNOWLEDGEMENTS
206 Original development sponsored by L<http://www.altinity.com/>
210 L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>,
211 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
215 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
219 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
220 at rt.cpan.org>, or through the web interface at
221 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
222 I will be notified, and then you'll automatically be notified of progress on
223 your bug as I make changes.
231 =item * RT: CPAN's request tracker
233 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
235 =item * AnnoCPAN: Annotated CPAN documentation
237 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
241 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
245 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
249 =head1 COPYRIGHT & LICENSE
251 Copyright (c) 2008 Matt S. Trout
253 This program is free software; you can redistribute it and/or modify it
254 under the same terms as Perl itself.
258 1; # End of Catalyst::Controller::WrapCGI
260 # vim: expandtab shiftwidth=2 ts=2 tw=80: