Working on a dist for WrapCGI...
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / WrapCGI.pm
CommitLineData
71e6daf6 1package Catalyst::Controller::WrapCGI;
b2a17df2 2
b2a17df2 3use strict;
4use warnings;
32b32c62 5use parent 'Catalyst::Controller';
b2a17df2 6
7use HTTP::Request::AsCGI;
8use HTTP::Request;
32b32c62 9use URI;
10
11=head1 NAME
12
13Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
14
15=head1 VERSION
16
17Version 0.001
18
19=cut
20
21our $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
b2a17df2 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
32b32c62 46open my $REAL_STDIN, "<&=".fileno(*STDIN);
47open my $REAL_STDOUT, ">>&=".fileno(*STDOUT);
48
49=head1 METHODS
50
51=head2 $self->cgi_to_response($c, $coderef)
52
53Does the magic of running $coderef in a CGI environment, and populating the
54appropriate parts of your Catalyst context with the results.
55
56=cut
b2a17df2 57
58sub 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
32b32c62 65 my $location = $res->headers->header('Location');
66
67 if (defined $location && length $location && $res->code == 200) {
b2a17df2 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
32b32c62 76=head2 $self->wrap_cgi($c, $coderef)
77
78Runs $coderef in a CGI environment using L<HTTP::Request::AsCGI>, returns an
79L<HTTP::Response>.
80
81The CGI environment is set up based on $c.
82
83Used by cgi_to_response, which is probably what you want to use as well.
84
85=cut
86
b2a17df2 87sub 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;
32b32c62 101 if (%$body_params) {
102 my $encoder = URI->new;
103 $encoder->query_form(%$body_params);
104 $body_content = $encoder->query;
b2a17df2 105 $req->content_type('application/x-www-form-urlencoded');
106 }
107 }
108
b2a17df2 109 $req->content($body_content);
110 $req->content_length(length($body_content));
111 my $user = (($c->can('user_exists') && $c->user_exists)
32b32c62 112 ? eval { $c->user->obj->username }
b2a17df2 113 : '');
114 my $env = HTTP::Request::AsCGI->new(
115 $req,
116 REMOTE_USER => $user,
32b32c62 117 %ENV
b2a17df2 118 );
119
120 {
32b32c62 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
b2a17df2 123
32b32c62 124 my $old = select($REAL_STDOUT); # in case somebody just calls 'print'
b2a17df2 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
32b32c62 142=head1 ACKNOWLEDGEMENTS
143
144Original development sponsored by L<http://www.altinity.com/>
145
146=head1 AUTHOR
147
148Matt S. Trout, C<< <mst at shadowcat.co.uk> >>
149
150=head1 BUGS
151
152Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi
153at rt.cpan.org>, or through the web interface at
154L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
155I will be notified, and then you'll automatically be notified of progress on
156your bug as I make changes.
157
158=head1 SUPPORT
159
160More information at:
161
162=over 4
163
164=item * RT: CPAN's request tracker
165
166L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
167
168=item * AnnoCPAN: Annotated CPAN documentation
169
170L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
171
172=item * CPAN Ratings
173
174L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
175
176=item * Search CPAN
177
178L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
179
180=back
181
182=head1 COPYRIGHT & LICENSE
183
184Copyright (c) 2008 Matt S. Trout
185
186This program is free software; you can redistribute it and/or modify it
187under the same terms as Perl itself.
188
189=cut
190
1911; # End of Catalyst::Controller::WrapCGI
192
193# vim: expandtab shiftwidth=4 ts=4 tw=80: