3e9d1174566db3139ff29d6a80338ca2986ca5fe
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / WrapCGI.pm
1 package Catalyst::Controller::WrapCGI;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use HTTP::Request::AsCGI;
8 use HTTP::Request;
9 use URI;
10
11 =head1 NAME
12
13 Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
14
15 =head1 VERSION
16
17 Version 0.001
18
19 =cut
20
21 our $VERSION = '0.001';
22
23 =head1 SYNOPSIS
24
25     package MyApp::Controller::Foo;
26
27     use parent qw/Catalyst::Controller::WrapCGI/;
28
29     sub hello : Path('cgi-bin/hello.cgi') {
30         my ($self, $c) = @_;
31
32         $self->cgi_to_response($c, sub {
33             use CGI ':standard';
34
35             print header, start_html('Hello'),
36                 h1('Catalyst Rocks!'),
37                 end_html;
38         });
39     }
40
41 =cut
42
43 # Hack-around because Catalyst::Engine::HTTP goes and changes
44 # them to be the remote socket, and FCGI.pm does even dumber things.
45
46 open my $REAL_STDIN, "<&=".fileno(*STDIN);
47 open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
48
49 =head1 METHODS
50
51 =head2 $self->cgi_to_response($c, $coderef)
52
53 Does the magic of running $coderef in a CGI environment, and populating the
54 appropriate parts of your Catalyst context with the results.
55
56 =cut
57
58 sub cgi_to_response {
59   my ($self, $c, $script) = @_;
60   my $res = $self->wrap_cgi($c, $script);
61
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
64
65   my $location = $res->headers->header('Location');
66
67   if (defined $location && length $location && $res->code == 200) {
68     $c->res->status(302);
69   } else { 
70     $c->res->status($res->code);
71   }
72   $c->res->body($res->content);
73   $c->res->headers($res->headers);
74 }
75
76 =head2 $self->wrap_cgi($c, $coderef)
77
78 Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
79 L<HTTP::Response>.
80
81 The CGI environment is set up based on $c.
82
83 Used by cgi_to_response, which is probably what you want to use as well.
84
85 =cut
86
87 sub wrap_cgi {
88   my ($self, $c, $call) = @_;
89   my $req = HTTP::Request->new(
90     map { $c->req->$_ } qw/method uri headers/
91   );
92   my $body = $c->req->body;
93   my $body_content = '';
94
95   $req->content_type($c->req->content_type); # set this now so we can override
96
97   if ($body) { # Slurp from body filehandle
98     local $/; $body_content = <$body>;
99   } else {
100     my $body_params = $c->req->body_parameters;
101     if (%$body_params) {
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');
106     }
107   }
108
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 }
113                 : '');
114   my $env = HTTP::Request::AsCGI->new(
115               $req,
116               REMOTE_USER => $user,
117               %ENV
118             );
119
120   {
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
123
124     my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
125
126     my $saved_error;
127
128     $env->setup;
129     eval { $call->() };
130     $saved_error = $@;
131     $env->restore;
132
133     select($old);
134
135     warn "CGI invoke failed: $saved_error" if $saved_error;
136
137   }
138
139   return $env->response;
140 }
141
142 =head1 ACKNOWLEDGEMENTS
143
144 Original development sponsored by L<http://www.altinity.com/>
145
146 =head1 AUTHOR
147
148 Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
149
150 =head1 BUGS
151
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.
157
158 =head1 SUPPORT
159
160 More information at:
161
162 =over 4
163
164 =item * RT: CPAN's request tracker
165
166 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
167
168 =item * AnnoCPAN: Annotated CPAN documentation
169
170 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
171
172 =item * CPAN Ratings
173
174 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
175
176 =item * Search CPAN
177
178 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
179
180 =back
181
182 =head1 COPYRIGHT & LICENSE
183
184 Copyright (c) 2008 Matt S. Trout
185
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
188
189 =cut
190
191 1; # End of Catalyst::Controller::WrapCGI
192
193 # vim: expandtab shiftwidth=4 ts=4 tw=80: