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!'),
43 # Hack-around because Catalyst::Engine::HTTP goes and changes
44 # them to be the remote socket, and FCGI.pm does even dumber things.
46 open my $REAL_STDIN, "<&=".fileno(*STDIN);
47 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
51 =head2 $self->cgi_to_response($c, $coderef)
53 Does the magic of running $coderef in a CGI environment, and populating the
54 appropriate parts of your Catalyst context with the results.
59 my ($self, $c, $script) = @_;
60 my $res = $self->wrap_cgi($c, $script);
62 # if the CGI doesn't set the response code but sets location they were
63 # probably trying to redirect so set 302 for them
65 my $location = $res->headers->header('Location');
67 if (defined $location && length $location && $res->code == 200) {
70 $c->res->status($res->code);
72 $c->res->body($res->content);
73 $c->res->headers($res->headers);
76 =head2 $self->wrap_cgi($c, $coderef)
78 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
81 The CGI environment is set up based on $c.
83 Used by cgi_to_response, which is probably what you want to use as well.
88 my ($self, $c, $call) = @_;
89 my $req = HTTP::Request->new(
90 map { $c->req->$_ } qw/method uri headers/
92 my $body = $c->req->body;
93 my $body_content = '';
95 $req->content_type($c->req->content_type); # set this now so we can override
97 if ($body) { # Slurp from body filehandle
98 local $/; $body_content = <$body>;
100 my $body_params = $c->req->body_parameters;
102 my $encoder = URI->new;
103 $encoder->query_form(%$body_params);
104 $body_content = $encoder->query;
105 $req->content_type('application/x-www-form-urlencoded');
109 $req->content($body_content);
110 $req->content_length(length($body_content));
111 my $user = (($c->can('user_exists') && $c->user_exists)
112 ? eval { $c->user->obj->username }
114 my $env = HTTP::Request::AsCGI->new(
116 REMOTE_USER => $user,
121 local *STDIN = $REAL_STDIN; # restore the real ones so the filenos
122 local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup
124 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
135 warn "CGI invoke failed: $saved_error" if $saved_error;
139 return $env->response;
142 =head1 ACKNOWLEDGEMENTS
144 Original development sponsored by L<http://www.altinity.com/>
148 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
152 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
153 at rt.cpan.org>, or through the web interface at
154 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
155 I will be notified, and then you'll automatically be notified of progress on
156 your bug as I make changes.
164 =item * RT: CPAN's request tracker
166 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
168 =item * AnnoCPAN: Annotated CPAN documentation
170 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
174 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
178 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
182 =head1 COPYRIGHT & LICENSE
184 Copyright (c) 2008 Matt S. Trout
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
191 1; # End of Catalyst::Controller::WrapCGI
193 # vim: expandtab shiftwidth=4 ts=4 tw=80: