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.002';
26 package MyApp::Controller::Foo;
28 use parent qw/Catalyst::Controller::WrapCGI/;
30 sub hello : Path('cgi-bin/hello.cgi') {
33 $self->cgi_to_response($c, sub {
36 print header, start_html('Hello'),
37 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 If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>.
62 C<$your_controller->{CGI}{pass_env}> should be an array of environment variables
63 or regular expressions to pass through to your CGIs. Entries surrounded by C</>
64 characters are considered regular expressions.
66 Default is to pass the whole of C<%ENV>.
68 C<{CGI}{username_field}> should be the field for your user's name, which will be
69 read from C<$c->user->obj>. Defaults to 'username'.
71 See L</SYNOPSIS> for an example.
75 # Hack-around because Catalyst::Engine::HTTP goes and changes
76 # them to be the remote socket, and FCGI.pm does even dumber things.
78 open my $REAL_STDIN, "<&=".fileno(*STDIN);
79 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
83 =head2 $self->cgi_to_response($c, $coderef)
85 Does the magic of running $coderef in a CGI environment, and populating the
86 appropriate parts of your Catalyst context with the results.
88 Calls wrap_cgi (below.)
93 my ($self, $c, $script) = @_;
94 my $res = $self->wrap_cgi($c, $script);
96 # if the CGI doesn't set the response code but sets location they were
97 # probably trying to redirect so set 302 for them
99 my $location = $res->headers->header('Location');
101 if (defined $location && length $location && $res->code == 200) {
102 $c->res->status(302);
104 $c->res->status($res->code);
106 $c->res->body($res->content);
107 $c->res->headers($res->headers);
110 =head2 $self->wrap_cgi($c, $coderef)
112 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
115 The CGI environment is set up based on $c.
117 The environment variables to pass on are taken from the configuration for your
118 Controller, see L</SYNOPSIS> for an example. If you don't supply a list of
119 environment variables to pass, the whole of %ENV is used.
121 Used by cgi_to_response (above), which is probably what you want to use as well.
126 my ($self, $c, $call) = @_;
127 my $req = HTTP::Request->new(
128 map { $c->req->$_ } qw/method uri headers/
130 my $body = $c->req->body;
131 my $body_content = '';
133 $req->content_type($c->req->content_type); # set this now so we can override
135 if ($body) { # Slurp from body filehandle
136 local $/; $body_content = <$body>;
138 my $body_params = $c->req->body_parameters;
140 my $encoder = URI->new;
141 $encoder->query_form(%$body_params);
142 $body_content = $encoder->query;
143 $req->content_type('application/x-www-form-urlencoded');
149 my $pass_env = $self->{CGI}{pass_env};
150 $pass_env = [] if not defined $pass_env;
151 $pass_env = [ $pass_env ] unless ref $pass_env;
156 push @env, grep /$re/, keys %ENV;
162 @env = keys %ENV unless @env;
164 $req->content($body_content);
165 $req->content_length(length($body_content));
167 my $username_field = $self->{CGI}{username_field} || 'username';
169 my $username = (($c->can('user_exists') && $c->user_exists)
170 ? eval { $c->user->obj->$username_field }
172 my $env = HTTP::Request::AsCGI->new(
174 ($username ? (REMOTE_USER => $username) : ()),
175 map { ($_, $ENV{$_}) } @env
179 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
180 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
182 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
193 Catalyst::Exception->throw(
194 message => "CGI invocation failed: $saved_error"
198 return $env->response;
201 =head1 ACKNOWLEDGEMENTS
203 Original development sponsored by L<http://www.altinity.com/>
207 L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>,
208 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
212 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
216 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
217 at rt.cpan.org>, or through the web interface at
218 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
219 I will be notified, and then you'll automatically be notified of progress on
220 your bug as I make changes.
228 =item * RT: CPAN's request tracker
230 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
232 =item * AnnoCPAN: Annotated CPAN documentation
234 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
238 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
242 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
246 =head1 COPYRIGHT & LICENSE
248 Copyright (c) 2008 Matt S. Trout
250 This program is free software; you can redistribute it and/or modify it
251 under the same terms as Perl itself.
255 1; # End of Catalyst::Controller::WrapCGI
257 # vim: expandtab shiftwidth=2 ts=2 tw=80: