Updated changes file
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
3use strict;
c2e8e6fa 4use base 'Catalyst::Engine::CGI::Base';
e7c0c583 5
6use CGI;
fc7ec1d9 7
e2fd5b5f 8__PACKAGE__->mk_accessors('cgi');
9
fc7ec1d9 10=head1 NAME
11
12Catalyst::Engine::CGI - The CGI Engine
13
14=head1 SYNOPSIS
15
23f9d934 16A script using the Catalyst::Engine::CGI module might look like:
17
9a33da6a 18 #!/usr/bin/perl -w
19
20 use strict;
21 use lib '/path/to/MyApp/lib';
22 use MyApp;
23
24 MyApp->run;
25
23f9d934 26The application module (C<MyApp>) would use C<Catalyst>, which loads the
27appropriate engine module.
fc7ec1d9 28
29=head1 DESCRIPTION
30
23f9d934 31This is the Catalyst engine specialized for the CGI environment (using the
e7c0c583 32C<CGI> and C<CGI::Cookie> modules). Normally Catalyst will select the
23f9d934 33appropriate engine according to the environment that it detects, however you
34can force Catalyst to use the CGI engine by specifying the following in your
35application module:
36
37 use Catalyst qw(-Engine=CGI);
fc7ec1d9 38
9a33da6a 39The performance of this way of using Catalyst is not expected to be
40useful in production applications, but it may be helpful for development.
41
e2fd5b5f 42=head1 METHODS
43
44=over 4
45
46=item $c->cgi
47
48Contains the C<CGI> object.
49
50=back
51
23f9d934 52=head1 OVERLOADED METHODS
fc7ec1d9 53
c2e8e6fa 54This class overloads some methods from C<Catalyst::Engine::CGI::Base>.
fc7ec1d9 55
23f9d934 56=over 4
57
06e1b616 58=item $c->prepare_body
fc7ec1d9 59
60=cut
61
06e1b616 62sub prepare_body {
fc7ec1d9 63 my $c = shift;
06e1b616 64
65 # XXX this is undocumented in CGI.pm. If Content-Type is not
66 # application/x-www-form-urlencoded or multipart/form-data
67 # CGI.pm will read STDIN into a param, POSTDATA.
68
e060fe05 69 $c->request->body( $c->cgi->param('POSTDATA') );
fc7ec1d9 70}
71
23f9d934 72=item $c->prepare_parameters
fc7ec1d9 73
74=cut
75
76sub prepare_parameters {
e7c0c583 77 my $c = shift;
bfde09a2 78
5b387dfc 79 my ( @params );
bfde09a2 80
b9e9fff6 81 if ( $c->request->method eq 'POST' ) {
b9e9fff6 82 for my $param ( $c->cgi->url_param ) {
83 for my $value ( $c->cgi->url_param($param) ) {
84 push ( @params, $param, $value );
85 }
5b387dfc 86 }
fc7ec1d9 87 }
08cf3dd6 88
bfde09a2 89 for my $param ( $c->cgi->param ) {
08cf3dd6 90 for my $value ( $c->cgi->param($param) ) {
5b387dfc 91 push ( @params, $param, $value );
92 }
93 }
bfde09a2 94
95 $c->request->param(@params);
fc7ec1d9 96}
97
23f9d934 98=item $c->prepare_request
fc7ec1d9 99
100=cut
101
bfde09a2 102sub prepare_request {
3f822a28 103 my ( $c, $cgi ) = @_;
104 $c->cgi( $cgi || CGI->new );
e7c0c583 105 $c->cgi->_reset_globals;
106}
fc7ec1d9 107
23f9d934 108=item $c->prepare_uploads
fc7ec1d9 109
110=cut
111
112sub prepare_uploads {
113 my $c = shift;
e7c0c583 114
115 my @uploads;
bfde09a2 116
e7c0c583 117 for my $param ( $c->cgi->param ) {
bfde09a2 118
e7c0c583 119 my @values = $c->cgi->param($param);
120
121 next unless ref( $values[0] );
122
123 for my $fh (@values) {
124
125 next unless my $size = ( stat $fh )[7];
126
127 my $info = $c->cgi->uploadInfo($fh);
128 my $tempname = $c->cgi->tmpFileName($fh);
129 my $type = $info->{'Content-Type'};
130 my $disposition = $info->{'Content-Disposition'};
131 my $filename = ( $disposition =~ / filename="([^;]*)"/ )[0];
132
146554c5 133 my $upload = Catalyst::Request::Upload->new(
e7c0c583 134 filename => $filename,
135 size => $size,
136 tempname => $tempname,
137 type => $type
146554c5 138 );
bfde09a2 139
e7c0c583 140 push( @uploads, $param, $upload );
141 }
fc7ec1d9 142 }
bfde09a2 143
144 $c->request->upload(@uploads);
fc7ec1d9 145}
146
23f9d934 147=back
148
fc7ec1d9 149=head1 SEE ALSO
150
c2e8e6fa 151L<Catalyst> L<Catalyst::Engine> L<Catalyst::Engine::CGI::Base>.
fc7ec1d9 152
153=head1 AUTHOR
154
155Sebastian Riedel, C<sri@cpan.org>
c2e8e6fa 156Christian Hansen, C<ch@ngmedia.com>
fc7ec1d9 157
158=head1 COPYRIGHT
159
160This program is free software, you can redistribute it and/or modify it under
161the same terms as Perl itself.
162
163=cut
164
1651;