1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
9 $CGI::Simple::POST_MAX = 1048576;
10 $CGI::Simple::DISABLE_UPLOADS = 0;
12 __PACKAGE__->mk_accessors('cgi');
16 Catalyst::Engine::CGI - The CGI Engine
20 A script using the Catalyst::Engine::CGI module might look like:
25 use lib '/path/to/MyApp/lib';
30 The application module (C<MyApp>) would use C<Catalyst>, which loads the
31 appropriate engine module.
35 This is the Catalyst engine specialized for the CGI environment (using the
36 C<CGI::Simple> and C<CGI::Cookie> modules). Normally Catalyst will select the
37 appropriate engine according to the environment that it detects, however you
38 can force Catalyst to use the CGI engine by specifying the following in your
41 use Catalyst qw(-Engine=CGI);
43 The performance of this way of using Catalyst is not expected to be
44 useful in production applications, but it may be helpful for development.
52 This config parameter contains the C<CGI::Simple> object.
56 =head1 OVERLOADED METHODS
58 This class overloads some methods from C<Catalyst::Engine>.
62 =item $c->finalize_headers
66 sub finalize_headers {
70 $headers{-status} = $c->response->status if $c->response->status;
72 for my $name ( $c->response->headers->header_field_names ) {
73 $headers{"-$name"} = $c->response->header($name);
76 print $c->cgi->header(%headers);
79 =item $c->finalize_output
81 Prints the response output to STDOUT.
87 print $c->response->output;
90 =item $c->prepare_connection
94 sub prepare_connection {
96 $c->req->hostname( $c->cgi->remote_host );
97 $c->req->address( $c->cgi->remote_addr );
100 =item $c->prepare_headers
104 sub prepare_headers {
106 $c->req->method( $c->cgi->request_method );
107 for my $header ( $c->cgi->http ) {
108 ( my $field = $header ) =~ s/^HTTPS?_//;
109 $c->req->headers->header( $field => $c->cgi->http($header) );
111 $c->req->headers->header( 'Content-Type' => $c->cgi->content_type );
112 $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
115 =item $c->prepare_parameters
119 sub prepare_parameters {
122 $c->cgi->parse_query_string;
124 my %vars = $c->cgi->Vars;
125 while ( my ( $key, $value ) = each %vars ) {
126 my @values = split "\0", $value;
127 $vars{$key} = @values <= 1 ? $values[0] : \@values;
129 $c->req->parameters( {%vars} );
132 =item $c->prepare_path
141 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
142 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
143 my $port = $ENV{SERVER_PORT} || 80;
144 my $path = $ENV{SCRIPT_NAME} || '/';
147 $base->scheme($scheme);
152 $base = $base->canonical->as_string;
155 my $path = $ENV{PATH_INFO} || '/';
156 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
159 $c->req->base($base);
160 $c->req->path($path);
163 =item $c->prepare_request
167 sub prepare_request { shift->cgi( CGI::Simple->new ) }
169 =item $c->prepare_uploads
173 sub prepare_uploads {
175 for my $name ( $c->cgi->upload ) {
176 next unless defined $name;
177 $c->req->uploads->{$name} = {
178 fh => $c->cgi->upload($name),
179 size => $c->cgi->upload_info( $name, 'size' ),
180 type => $c->cgi->upload_info( $name, 'mime' )
189 sub run { shift->handler }
199 Sebastian Riedel, C<sri@cpan.org>
203 This program is free software, you can redistribute it and/or modify it under
204 the same terms as Perl itself.