1 package CatalystX::Controller::WrapCGI;
3 # AUTHOR: Matt S Trout, mst@shadowcatsystems.co.uk
4 # Original development sponsored by http://www.altinity.com/
8 use base 'Catalyst::Controller';
10 use HTTP::Request::AsCGI;
14 # Hack-around because Catalyst::Engine::HTTP goes and changes
15 # them to be the remote socket, and FCGI.pm does even dumber things.
17 open(*REAL_STDIN, "<&=".fileno(*STDIN));
18 open(*REAL_STDOUT, ">>&=".fileno(*STDOUT));
21 my ($self, $c, $script) = @_;
22 my $res = $self->wrap_cgi($c, $script);
24 # if the CGI doesn't set the response code but sets location they were
25 # probably trying to redirect so set 302 for them
27 my $location = $res->headers->header('Location');
29 if (defined $location && length $location && $res->code == 200) {
32 $c->res->status($res->code);
34 $c->res->body($res->content);
35 $c->res->headers($res->headers);
39 my ($self, $c, $call) = @_;
40 my $req = HTTP::Request->new(
41 map { $c->req->$_ } qw/method uri headers/
43 my $body = $c->req->body;
44 my $body_content = '';
46 $req->content_type($c->req->content_type); # set this now so we can override
48 if ($body) { # Slurp from body filehandle
49 local $/; $body_content = <$body>;
51 my $body_params = $c->req->body_parameters;
53 my $encoder = URI->new;
54 $encoder->query_form(%$body_params);
55 $body_content = $encoder->query;
56 $req->content_type('application/x-www-form-urlencoded');
60 $req->content($body_content);
61 $req->content_length(length($body_content));
62 my $user = (($c->can('user_exists') && $c->user_exists)
63 ? eval { $c->user->obj->username }
65 my $env = HTTP::Request::AsCGI->new(
72 local *STDIN = \*REAL_STDIN; # restore the real ones so the filenos
73 local *STDOUT = \*REAL_STDOUT; # are 0 and 1 for the env setup
75 my $old = select(REAL_STDOUT); # in case somebody just calls 'print'
86 warn "CGI invoke failed: $saved_error" if $saved_error;
90 return $env->response;