1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
10 __PACKAGE__->mk_accessors('cgi');
14 Catalyst::Engine::CGI - The CGI Engine
18 A script using the Catalyst::Engine::CGI module might look like:
23 use lib '/path/to/MyApp/lib';
28 The application module (C<MyApp>) would use C<Catalyst>, which loads the
29 appropriate engine module.
33 This is the Catalyst engine specialized for the CGI environment (using the
34 C<CGI> and C<CGI::Cookie> modules). Normally Catalyst will select the
35 appropriate engine according to the environment that it detects, however you
36 can force Catalyst to use the CGI engine by specifying the following in your
39 use Catalyst qw(-Engine=CGI);
41 The performance of this way of using Catalyst is not expected to be
42 useful in production applications, but it may be helpful for development.
50 This config parameter contains the C<CGI> object.
54 =head1 OVERLOADED METHODS
56 This class overloads some methods from C<Catalyst::Engine>.
60 =item $c->finalize_headers
64 sub finalize_headers {
67 $c->response->header( Status => $c->response->status );
69 print $c->response->headers->as_string("\015\012");
73 =item $c->finalize_output
75 Prints the response output to STDOUT.
81 print $c->response->output;
84 =item $c->prepare_connection
88 sub prepare_connection {
90 $c->req->hostname( $ENV{REMOTE_HOST} );
91 $c->req->address( $ENV{REMOTE_ADDR} );
94 =item $c->prepare_headers
101 while ( my ( $header, $value ) = each %ENV ) {
103 next unless $header =~ /^(HTTP|CONTENT)/i;
105 ( my $field = $header ) =~ s/^HTTPS?_//;
107 $c->req->headers->header( $field => $value );
110 $c->req->method( $ENV{REQUEST_METHOD} || 'GET' );
113 =item $c->prepare_parameters
117 sub prepare_parameters {
122 for my $param ( $c->cgi->url_param ) {
123 for my $value ( $c->cgi->url_param($param) ) {
124 push ( @params, $param, $value );
128 for my $param ( $c->cgi->param ) {
129 for my $value ( $c->cgi->param($param) ) {
130 push ( @params, $param, $value );
134 $c->req->_assign_values( $c->req->parameters, \@params );
137 =item $c->prepare_path
146 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
147 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
148 my $port = $ENV{SERVER_PORT} || 80;
149 my $path = $ENV{SCRIPT_NAME} || '/';
152 $base->scheme($scheme);
157 $base = $base->canonical->as_string;
160 my $path = $ENV{PATH_INFO} || '/';
161 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
164 $c->req->base($base);
165 $c->req->path($path);
168 =item $c->prepare_request
172 sub prepare_request {
175 $c->cgi->_reset_globals;
178 =item $c->prepare_uploads
182 sub prepare_uploads {
187 for my $param ( $c->cgi->param ) {
189 my @values = $c->cgi->param($param);
191 next unless ref( $values[0] );
193 for my $fh (@values) {
195 next unless my $size = ( stat $fh )[7];
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];
203 my $upload = Catalyst::Request::Upload->new(
204 filename => $filename,
206 tempname => $tempname,
210 push( @uploads, $param, $upload );
214 $c->req->_assign_values( $c->req->uploads, \@uploads );
221 sub run { shift->handler }
231 Sebastian Riedel, C<sri@cpan.org>
235 This program is free software, you can redistribute it and/or modify it under
236 the same terms as Perl itself.