1 package Catalyst::Controller::WrapCGI;
5 use parent 'Catalyst::Controller';
7 use HTTP::Request::AsCGI;
13 Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
21 our $VERSION = '0.001';
25 package MyApp::Controller::Foo;
27 use parent qw/Catalyst::Controller::WrapCGI/;
29 sub hello : Path('cgi-bin/hello.cgi') {
32 $self->cgi_to_response($c, sub {
35 print header, start_html('Hello'),
36 h1('Catalyst Rocks!'),
41 In your .conf, configure which environment variables to pass:
53 Allows you to run Perl code in a CGI environment derived from your L<Catalyst>
58 # Hack-around because Catalyst::Engine::HTTP goes and changes
59 # them to be the remote socket, and FCGI.pm does even dumber things.
61 open my $REAL_STDIN, "<&=".fileno(*STDIN);
62 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
66 =head2 $self->cgi_to_response($c, $coderef)
68 Does the magic of running $coderef in a CGI environment, and populating the
69 appropriate parts of your Catalyst context with the results.
71 Calls wrap_cgi (below.)
76 my ($self, $c, $script) = @_;
77 my $res = $self->wrap_cgi($c, $script);
79 # if the CGI doesn't set the response code but sets location they were
80 # probably trying to redirect so set 302 for them
82 my $location = $res->headers->header('Location');
84 if (defined $location && length $location && $res->code == 200) {
87 $c->res->status($res->code);
89 $c->res->body($res->content);
90 $c->res->headers($res->headers);
93 =head2 $self->wrap_cgi($c, $coderef)
95 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
98 The CGI environment is set up based on $c.
100 The environment variables to pass on are taken from the configuration for your
101 Controller, see L</SYNOPSIS> for an example. If you don't supply a list of
102 environment variables to pass, the whole of %ENV is used.
104 Used by cgi_to_response, which is probably what you want to use as well.
109 my ($self, $c, $call) = @_;
110 my $req = HTTP::Request->new(
111 map { $c->req->$_ } qw/method uri headers/
113 my $body = $c->req->body;
114 my $body_content = '';
116 $req->content_type($c->req->content_type); # set this now so we can override
118 if ($body) { # Slurp from body filehandle
119 local $/; $body_content = <$body>;
121 my $body_params = $c->req->body_parameters;
123 my $encoder = URI->new;
124 $encoder->query_form(%$body_params);
125 $body_content = $encoder->query;
126 $req->content_type('application/x-www-form-urlencoded');
130 my @env = @{ $self->{CGI}{pass_env} || [ keys %ENV ] };
132 $req->content($body_content);
133 $req->content_length(length($body_content));
134 my $user = (($c->can('user_exists') && $c->user_exists)
135 ? eval { $c->user->obj->username }
137 my $env = HTTP::Request::AsCGI->new(
139 REMOTE_USER => $user,
140 map { ($_, $ENV{$_}) } @env
144 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
145 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
147 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
158 warn "CGI invoke failed: $saved_error" if $saved_error;
162 return $env->response;
165 =head1 ACKNOWLEDGEMENTS
167 Original development sponsored by L<http://www.altinity.com/>
171 L<Catalyst::Plugin::CGIBin>, L<CatalystX::GlobalContext>,
172 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
176 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
180 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
181 at rt.cpan.org>, or through the web interface at
182 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
183 I will be notified, and then you'll automatically be notified of progress on
184 your bug as I make changes.
192 =item * RT: CPAN's request tracker
194 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
196 =item * AnnoCPAN: Annotated CPAN documentation
198 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
202 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
206 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
210 =head1 COPYRIGHT & LICENSE
212 Copyright (c) 2008 Matt S. Trout
214 This program is free software; you can redistribute it and/or modify it
215 under the same terms as Perl itself.
219 1; # End of Catalyst::Controller::WrapCGI
221 # vim: expandtab shiftwidth=4 ts=4 tw=80: