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 Catalyst::Engine::CGI generates a full set of HTTP headers, which means that
45 applications using the engine must be be configured as "Non-parsed Headers"
46 scripts (at least when running under Apache). To configure this under Apache
47 name the starting with C<nph->.
49 The performance of this way of using Catalyst is not expected to be
50 useful in production applications, but it may be helpful for development.
58 This config parameter contains the C<CGI::Simple> object.
62 =head1 OVERLOADED METHODS
64 This class overloads some methods from C<Catalyst::Engine>.
68 =item $c->finalize_headers
72 sub finalize_headers {
75 $headers{-status} = $c->response->status if $c->response->status;
76 for my $name ( $c->response->headers->header_field_names ) {
77 $headers{"-$name"} = $c->response->headers->header($name);
80 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
81 push @cookies, $c->cgi->cookie(
83 -value => $cookie->{value},
84 -expires => $cookie->{expires},
85 -domain => $cookie->{domain},
86 -path => $cookie->{path},
87 -secure => $cookie->{secure} || 0
90 $headers{-cookie} = \@cookies if @cookies;
91 print $c->cgi->header(%headers);
94 =item $c->finalize_output
96 Prints the response output to STDOUT.
100 sub finalize_output {
102 print $c->response->output;
105 =item $c->prepare_connection
109 sub prepare_connection {
111 $c->req->hostname( $c->cgi->remote_host );
112 $c->req->address( $c->cgi->remote_addr );
115 =item $c->prepare_cookies
121 sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) }
123 =item $c->prepare_headers
127 sub prepare_headers {
129 $c->req->method( $c->cgi->request_method );
130 for my $header ( $c->cgi->http ) {
131 ( my $field = $header ) =~ s/^HTTPS?_//;
132 $c->req->headers->header( $field => $c->cgi->http($header) );
134 $c->req->headers->header( 'Content-Type' => $c->cgi->content_type );
135 $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
138 =item $c->prepare_parameters
142 sub prepare_parameters {
145 $c->cgi->parse_query_string;
147 my %vars = $c->cgi->Vars;
148 while ( my ( $key, $value ) = each %vars ) {
149 my @values = split "\0", $value;
150 $vars{$key} = @values <= 1 ? $values[0] : \@values;
152 $c->req->parameters( {%vars} );
155 =item $c->prepare_path
164 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
165 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
166 my $port = $ENV{SERVER_PORT} || 80;
167 my $path = $ENV{SCRIPT_NAME} || '/';
170 $base->scheme($scheme);
175 $base = $base->canonical->as_string;
178 my $path = $ENV{PATH_INFO} || '/';
181 $c->req->base($base);
182 $c->req->path($path);
185 =item $c->prepare_request
189 sub prepare_request { shift->cgi( CGI::Simple->new ) }
191 =item $c->prepare_uploads
195 sub prepare_uploads {
197 for my $name ( $c->cgi->upload ) {
198 next unless defined $name;
199 $c->req->uploads->{$name} = {
200 fh => $c->cgi->upload($name),
201 size => $c->cgi->upload_info( $name, 'size' ),
202 type => $c->cgi->upload_info( $name, 'mime' )
211 sub run { shift->handler }
221 Sebastian Riedel, C<sri@cpan.org>
225 This program is free software, you can redistribute it and/or modify it under
226 the same terms as Perl itself.