1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
10 $CGI::Simple::POST_MAX = 1048576;
11 $CGI::Simple::DISABLE_UPLOADS = 0;
13 __PACKAGE__->mk_accessors('cgi');
17 Catalyst::Engine::CGI - The CGI Engine
21 A script using the Catalyst::Engine::CGI module might look like:
26 use lib '/path/to/MyApp/lib';
31 The application module (C<MyApp>) would use C<Catalyst>, which loads the
32 appropriate engine module.
36 This is the Catalyst engine specialized for the CGI environment (using the
37 C<CGI::Simple> and C<CGI::Cookie> modules). Normally Catalyst will select the
38 appropriate engine according to the environment that it detects, however you
39 can force Catalyst to use the CGI engine by specifying the following in your
42 use Catalyst qw(-Engine=CGI);
44 The performance of this way of using Catalyst is not expected to be
45 useful in production applications, but it may be helpful for development.
53 This config parameter contains the C<CGI::Simple> object.
57 =head1 OVERLOADED METHODS
59 This class overloads some methods from C<Catalyst::Engine>.
63 =item $c->finalize_headers
67 sub finalize_headers {
71 $headers{-status} = $c->response->status if $c->response->status;
73 for my $name ( $c->response->headers->header_field_names ) {
74 $headers{"-$name"} = $c->response->header($name);
77 print $c->cgi->header(%headers);
80 =item $c->finalize_output
82 Prints the response output to STDOUT.
88 print $c->response->output;
91 =item $c->prepare_connection
95 sub prepare_connection {
97 $c->req->hostname( $c->cgi->remote_host );
98 $c->req->address( $c->cgi->remote_addr );
101 =item $c->prepare_headers
105 sub prepare_headers {
107 $c->req->method( $c->cgi->request_method );
108 for my $header ( $c->cgi->http ) {
109 ( my $field = $header ) =~ s/^HTTPS?_//;
110 $c->req->headers->header( $field => $c->cgi->http($header) );
112 $c->req->headers->header( 'Content-Type' => $c->cgi->content_type );
113 $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
116 =item $c->prepare_parameters
120 sub prepare_parameters {
123 $c->cgi->parse_query_string;
125 my %vars = $c->cgi->Vars;
126 while ( my ( $key, $value ) = each %vars ) {
127 my @values = split "\0", $value;
128 $vars{$key} = @values <= 1 ? $values[0] : \@values;
130 $c->req->parameters( {%vars} );
133 =item $c->prepare_path
142 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
143 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
144 my $port = $ENV{SERVER_PORT} || 80;
145 my $path = $ENV{SCRIPT_NAME} || '/';
148 $base->scheme($scheme);
153 $base = $base->canonical->as_string;
156 my $path = $ENV{PATH_INFO} || '/';
157 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
160 $c->req->base($base);
161 $c->req->path($path);
164 =item $c->prepare_request
168 sub prepare_request { shift->cgi( CGI::Simple->new ) }
170 =item $c->prepare_uploads
174 sub prepare_uploads {
176 for my $name ( $c->cgi->upload ) {
177 next unless defined $name;
178 $c->req->uploads->{$name} = {
179 fh => $c->cgi->upload($name),
180 size => $c->cgi->upload_info( $name, 'size' ),
181 type => $c->cgi->upload_info( $name, 'mime' )
190 sub run { shift->handler }
200 Sebastian Riedel, C<sri@cpan.org>
204 This program is free software, you can redistribute it and/or modify it under
205 the same terms as Perl itself.