Commit | Line | Data |
71e6daf6 |
1 | package Catalyst::Controller::WrapCGI; |
b2a17df2 |
2 | |
b2a17df2 |
3 | use strict; |
4 | use warnings; |
32b32c62 |
5 | use parent 'Catalyst::Controller'; |
b2a17df2 |
6 | |
7 | use HTTP::Request::AsCGI; |
8 | use HTTP::Request; |
32b32c62 |
9 | use URI; |
21a20b7e |
10 | use Catalyst::Exception (); |
32b32c62 |
11 | |
12 | =head1 NAME |
13 | |
14 | Catalyst::Controller::WrapCGI - Run CGIs in Catalyst |
15 | |
16 | =head1 VERSION |
17 | |
21a20b7e |
18 | Version 0.002 |
32b32c62 |
19 | |
20 | =cut |
21 | |
21a20b7e |
22 | our $VERSION = '0.002'; |
32b32c62 |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | package MyApp::Controller::Foo; |
27 | |
28 | use parent qw/Catalyst::Controller::WrapCGI/; |
29 | |
30 | sub hello : Path('cgi-bin/hello.cgi') { |
31 | my ($self, $c) = @_; |
32 | |
33 | $self->cgi_to_response($c, sub { |
34 | use CGI ':standard'; |
35 | |
36 | print header, start_html('Hello'), |
37 | h1('Catalyst Rocks!'), |
38 | end_html; |
39 | }); |
40 | } |
41 | |
457c1d76 |
42 | In your .conf, configure which environment variables to pass: |
43 | |
44 | <Controller::Foo> |
45 | <CGI> |
21a20b7e |
46 | username_field username # used for REMOTE_USER env var |
457c1d76 |
47 | pass_env PERL5LIB |
48 | pass_env PATH |
21a20b7e |
49 | pass_env /^MYAPP_/ |
457c1d76 |
50 | </CGI> |
51 | </Controller::Foo> |
52 | |
53 | =head1 DESCRIPTION |
54 | |
55 | Allows you to run Perl code in a CGI environment derived from your L<Catalyst> |
56 | context. |
57 | |
21a20b7e |
58 | If you just want to run CGIs from files, see L<Catalyst::Controller::CGIBin>. |
59 | |
60 | =head1 CONFIGURATION |
61 | |
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. |
65 | |
66 | Default is to pass the whole of C<%ENV>. |
67 | |
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'. |
70 | |
71 | See L</SYNOPSIS> for an example. |
72 | |
32b32c62 |
73 | =cut |
b2a17df2 |
74 | |
75 | # Hack-around because Catalyst::Engine::HTTP goes and changes |
76 | # them to be the remote socket, and FCGI.pm does even dumber things. |
77 | |
32b32c62 |
78 | open my $REAL_STDIN, "<&=".fileno(*STDIN); |
79 | open my $REAL_STDOUT, ">>&=".fileno(*STDOUT); |
80 | |
81 | =head1 METHODS |
82 | |
83 | =head2 $self->cgi_to_response($c, $coderef) |
84 | |
85 | Does the magic of running $coderef in a CGI environment, and populating the |
86 | appropriate parts of your Catalyst context with the results. |
87 | |
457c1d76 |
88 | Calls wrap_cgi (below.) |
89 | |
32b32c62 |
90 | =cut |
b2a17df2 |
91 | |
92 | sub cgi_to_response { |
93 | my ($self, $c, $script) = @_; |
94 | my $res = $self->wrap_cgi($c, $script); |
95 | |
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 |
98 | |
32b32c62 |
99 | my $location = $res->headers->header('Location'); |
100 | |
101 | if (defined $location && length $location && $res->code == 200) { |
b2a17df2 |
102 | $c->res->status(302); |
103 | } else { |
104 | $c->res->status($res->code); |
105 | } |
106 | $c->res->body($res->content); |
107 | $c->res->headers($res->headers); |
108 | } |
109 | |
32b32c62 |
110 | =head2 $self->wrap_cgi($c, $coderef) |
111 | |
112 | Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an |
113 | L<HTTP::Response>. |
114 | |
115 | The CGI environment is set up based on $c. |
116 | |
457c1d76 |
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. |
120 | |
21a20b7e |
121 | Used by cgi_to_response (above), which is probably what you want to use as well. |
32b32c62 |
122 | |
123 | =cut |
124 | |
b2a17df2 |
125 | sub wrap_cgi { |
126 | my ($self, $c, $call) = @_; |
127 | my $req = HTTP::Request->new( |
128 | map { $c->req->$_ } qw/method uri headers/ |
129 | ); |
130 | my $body = $c->req->body; |
131 | my $body_content = ''; |
132 | |
133 | $req->content_type($c->req->content_type); # set this now so we can override |
134 | |
135 | if ($body) { # Slurp from body filehandle |
136 | local $/; $body_content = <$body>; |
137 | } else { |
138 | my $body_params = $c->req->body_parameters; |
32b32c62 |
139 | if (%$body_params) { |
140 | my $encoder = URI->new; |
141 | $encoder->query_form(%$body_params); |
142 | $body_content = $encoder->query; |
b2a17df2 |
143 | $req->content_type('application/x-www-form-urlencoded'); |
144 | } |
145 | } |
146 | |
21a20b7e |
147 | my @env; |
148 | |
149 | for (@{ $self->{CGI}{pass_env} }) { |
150 | if (m!^/(.*)/\z!) { |
151 | my $re = qr/$1/; |
152 | push @env, grep /$re/, keys %ENV; |
153 | } else { |
154 | push @env, $_; |
155 | } |
156 | } |
157 | |
158 | @env = keys %ENV unless @env; |
457c1d76 |
159 | |
b2a17df2 |
160 | $req->content($body_content); |
161 | $req->content_length(length($body_content)); |
21a20b7e |
162 | |
163 | my $username_field = $self->{CGI}{username_field} || 'username'; |
164 | |
165 | my $username = (($c->can('user_exists') && $c->user_exists) |
166 | ? eval { $c->user->obj->$username_field } |
b2a17df2 |
167 | : ''); |
168 | my $env = HTTP::Request::AsCGI->new( |
169 | $req, |
21a20b7e |
170 | ($username ? (REMOTE_USER => $username) : ()), |
457c1d76 |
171 | map { ($_, $ENV{$_}) } @env |
b2a17df2 |
172 | ); |
173 | |
174 | { |
32b32c62 |
175 | local *STDIN = $REAL_STDIN; # restore the real ones so the filenos |
176 | local *STDOUT = $REAL_STDOUT; # are 0 and 1 for the env setup |
b2a17df2 |
177 | |
32b32c62 |
178 | my $old = select($REAL_STDOUT); # in case somebody just calls 'print' |
b2a17df2 |
179 | |
180 | my $saved_error; |
181 | |
182 | $env->setup; |
183 | eval { $call->() }; |
184 | $saved_error = $@; |
185 | $env->restore; |
186 | |
187 | select($old); |
188 | |
21a20b7e |
189 | Catalyst::Exception->throw( |
190 | message => "CGI invocation failed: $saved_error" |
191 | ) if $saved_error; |
b2a17df2 |
192 | } |
193 | |
194 | return $env->response; |
195 | } |
196 | |
32b32c62 |
197 | =head1 ACKNOWLEDGEMENTS |
198 | |
199 | Original development sponsored by L<http://www.altinity.com/> |
200 | |
457c1d76 |
201 | =head1 SEE ALSO |
202 | |
21a20b7e |
203 | L<Catalyst::Controller::CGIBin>, L<CatalystX::GlobalContext>, |
457c1d76 |
204 | L<Catalyst::Controller>, L<CGI>, L<Catalyst> |
205 | |
32b32c62 |
206 | =head1 AUTHOR |
207 | |
208 | Matt S. Trout, C<< <mst at shadowcat.co.uk> >> |
209 | |
210 | =head1 BUGS |
211 | |
212 | Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi |
213 | at rt.cpan.org>, or through the web interface at |
214 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>. |
215 | I will be notified, and then you'll automatically be notified of progress on |
216 | your bug as I make changes. |
217 | |
218 | =head1 SUPPORT |
219 | |
220 | More information at: |
221 | |
222 | =over 4 |
223 | |
224 | =item * RT: CPAN's request tracker |
225 | |
226 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI> |
227 | |
228 | =item * AnnoCPAN: Annotated CPAN documentation |
229 | |
230 | L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI> |
231 | |
232 | =item * CPAN Ratings |
233 | |
234 | L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI> |
235 | |
236 | =item * Search CPAN |
237 | |
238 | L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI> |
239 | |
240 | =back |
241 | |
242 | =head1 COPYRIGHT & LICENSE |
243 | |
244 | Copyright (c) 2008 Matt S. Trout |
245 | |
246 | This program is free software; you can redistribute it and/or modify it |
247 | under the same terms as Perl itself. |
248 | |
249 | =cut |
250 | |
251 | 1; # End of Catalyst::Controller::WrapCGI |
252 | |
21a20b7e |
253 | # vim: expandtab shiftwidth=2 ts=2 tw=80: |