Fixed: builtin actions are private too (Tutorial Docs)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
3use strict;
4use base 'Catalyst::Engine';
e7c0c583 5
6use CGI;
fc7ec1d9 7use URI;
399ed680 8use URI::http;
fc7ec1d9 9
fc7ec1d9 10__PACKAGE__->mk_accessors('cgi');
11
12=head1 NAME
13
14Catalyst::Engine::CGI - The CGI Engine
15
16=head1 SYNOPSIS
17
23f9d934 18A script using the Catalyst::Engine::CGI module might look like:
19
9a33da6a 20 #!/usr/bin/perl -w
21
22 use strict;
23 use lib '/path/to/MyApp/lib';
24 use MyApp;
25
26 MyApp->run;
27
23f9d934 28The application module (C<MyApp>) would use C<Catalyst>, which loads the
29appropriate engine module.
fc7ec1d9 30
31=head1 DESCRIPTION
32
23f9d934 33This is the Catalyst engine specialized for the CGI environment (using the
e7c0c583 34C<CGI> and C<CGI::Cookie> modules). Normally Catalyst will select the
23f9d934 35appropriate engine according to the environment that it detects, however you
36can force Catalyst to use the CGI engine by specifying the following in your
37application module:
38
39 use Catalyst qw(-Engine=CGI);
fc7ec1d9 40
9a33da6a 41The performance of this way of using Catalyst is not expected to be
42useful in production applications, but it may be helpful for development.
43
23f9d934 44=head1 METHODS
fc7ec1d9 45
23f9d934 46=over 4
47
23f9d934 48=item $c->cgi
fc7ec1d9 49
e7c0c583 50This config parameter contains the C<CGI> object.
fc7ec1d9 51
23f9d934 52=back
53
54=head1 OVERLOADED METHODS
fc7ec1d9 55
45374ac6 56This class overloads some methods from C<Catalyst::Engine>.
fc7ec1d9 57
23f9d934 58=over 4
59
60=item $c->finalize_headers
fc7ec1d9 61
62=cut
63
64sub finalize_headers {
65 my $c = shift;
6dc87a0f 66
e7c0c583 67 $c->response->header( Status => $c->response->status );
6dc87a0f 68
e7c0c583 69 print $c->response->headers->as_string("\015\012");
70 print "\015\012";
fc7ec1d9 71}
72
23f9d934 73=item $c->finalize_output
74
75Prints the response output to STDOUT.
fc7ec1d9 76
77=cut
78
79sub finalize_output {
80 my $c = shift;
81 print $c->response->output;
82}
83
0556eb49 84=item $c->prepare_connection
85
86=cut
87
88sub prepare_connection {
89 my $c = shift;
e7c0c583 90 $c->req->hostname( $ENV{REMOTE_HOST} );
91 $c->req->address( $ENV{REMOTE_ADDR} );
0556eb49 92}
93
23f9d934 94=item $c->prepare_headers
fc7ec1d9 95
96=cut
97
98sub prepare_headers {
99 my $c = shift;
e7c0c583 100
101 while ( my ( $header, $value ) = each %ENV ) {
102
103 next unless $header =~ /^(HTTP|CONTENT)/i;
104
fc7ec1d9 105 ( my $field = $header ) =~ s/^HTTPS?_//;
e7c0c583 106
107 $c->req->headers->header( $field => $value );
fc7ec1d9 108 }
e7c0c583 109
110 $c->req->method( $ENV{REQUEST_METHOD} || 'GET' );
fc7ec1d9 111}
112
23f9d934 113=item $c->prepare_parameters
fc7ec1d9 114
115=cut
116
117sub prepare_parameters {
e7c0c583 118 my $c = shift;
5b387dfc 119
120 my ( @params );
e7c0c583 121
08cf3dd6 122 for my $param ( $c->cgi->url_param ) {
123 for my $value ( $c->cgi->url_param($param) ) {
5b387dfc 124 push ( @params, $param, $value );
125 }
fc7ec1d9 126 }
08cf3dd6 127
128 for my $param ( $c->cgi->param ) {
129 for my $value ( $c->cgi->param($param) ) {
5b387dfc 130 push ( @params, $param, $value );
131 }
132 }
08cf3dd6 133
5b387dfc 134 $c->req->_assign_values( $c->req->parameters, \@params );
fc7ec1d9 135}
136
23f9d934 137=item $c->prepare_path
fc7ec1d9 138
139=cut
140
141sub prepare_path {
142 my $c = shift;
8b4483b3 143
144 my $base;
145 {
146 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
e7c0c583 147 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
8b4483b3 148 my $port = $ENV{SERVER_PORT} || 80;
149 my $path = $ENV{SCRIPT_NAME} || '/';
150
151 $base = URI->new;
152 $base->scheme($scheme);
153 $base->host($host);
154 $base->port($port);
155 $base->path($path);
156
157 $base = $base->canonical->as_string;
7833fdfc 158 }
8b4483b3 159
160 my $path = $ENV{PATH_INFO} || '/';
6dc87a0f 161 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
e7c0c583 162 $path =~ s/^\///;
8b4483b3 163
164 $c->req->base($base);
165 $c->req->path($path);
fc7ec1d9 166}
167
23f9d934 168=item $c->prepare_request
fc7ec1d9 169
170=cut
171
e7c0c583 172sub prepare_request {
173 my $c = shift;
174 $c->cgi( CGI->new );
175 $c->cgi->_reset_globals;
176}
fc7ec1d9 177
23f9d934 178=item $c->prepare_uploads
fc7ec1d9 179
180=cut
181
182sub prepare_uploads {
183 my $c = shift;
e7c0c583 184
185 my @uploads;
186
187 for my $param ( $c->cgi->param ) {
188
189 my @values = $c->cgi->param($param);
190
191 next unless ref( $values[0] );
192
193 for my $fh (@values) {
194
195 next unless my $size = ( stat $fh )[7];
196
197 my $info = $c->cgi->uploadInfo($fh);
198 my $tempname = $c->cgi->tmpFileName($fh);
199 my $type = $info->{'Content-Type'};
200 my $disposition = $info->{'Content-Disposition'};
201 my $filename = ( $disposition =~ / filename="([^;]*)"/ )[0];
202
146554c5 203 my $upload = Catalyst::Request::Upload->new(
e7c0c583 204 filename => $filename,
205 size => $size,
206 tempname => $tempname,
207 type => $type
146554c5 208 );
e7c0c583 209
210 push( @uploads, $param, $upload );
211 }
fc7ec1d9 212 }
e7c0c583 213
214 $c->req->_assign_values( $c->req->uploads, \@uploads );
fc7ec1d9 215}
216
c9afa5fc 217=item $c->run
218
219=cut
220
fc7ec1d9 221sub run { shift->handler }
222
23f9d934 223=back
224
fc7ec1d9 225=head1 SEE ALSO
226
227L<Catalyst>.
228
229=head1 AUTHOR
230
231Sebastian Riedel, C<sri@cpan.org>
232
233=head1 COPYRIGHT
234
235This program is free software, you can redistribute it and/or modify it under
236the same terms as Perl itself.
237
238=cut
239
2401;