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 To be called from a CGI script to start the Catalyst application.
62 This config parameter contains the C<CGI::Simple> object.
66 =head1 OVERLOADED METHODS
68 This class overloads some methods from C<Catalyst>.
72 =item $c->finalize_headers
76 sub finalize_headers {
78 my %headers = ( -nph => 1 );
79 $headers{-status} = $c->response->status if $c->response->status;
80 for my $name ( $c->response->headers->header_field_names ) {
81 $headers{"-$name"} = $c->response->headers->header($name);
84 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
85 push @cookies, $c->cgi->cookie(
87 -value => $cookie->{value},
88 -expires => $cookie->{expires},
89 -domain => $cookie->{domain},
90 -path => $cookie->{path},
91 -secure => $cookie->{secure} || 0
94 $headers{-cookie} = \@cookies if @cookies;
95 print $c->cgi->header(%headers);
98 =item $c->finalize_output
100 Prints the response output to STDOUT.
104 sub finalize_output {
106 print $c->response->output;
109 =item $c->prepare_connection
113 sub prepare_connection {
115 $c->req->hostname( $c->cgi->remote_host );
116 $c->req->address( $c->cgi->remote_addr );
119 =item $c->prepare_cookies
125 sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) }
127 =item $c->prepare_headers
131 sub prepare_headers {
133 $c->req->method( $c->cgi->request_method );
134 for my $header ( $c->cgi->http ) {
135 ( my $field = $header ) =~ s/^HTTPS?_//;
136 $c->req->headers->header( $field => $c->cgi->http($header) );
138 $c->req->headers->header( 'Content-Type' => $c->cgi->content_type );
139 $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
142 =item $c->prepare_parameters
146 sub prepare_parameters {
148 my %vars = $c->cgi->Vars;
149 while ( my ( $key, $value ) = each %vars ) {
150 my @values = split "\0", $value;
151 $vars{$key} = @values <= 1 ? $values[0] : \@values;
153 $c->req->parameters( {%vars} );
156 =item $c->prepare_path
162 $c->req->path( $c->cgi->url( -absolute => 1, -path_info => 1 ) );
163 my $loc = $c->cgi->url( -absolute => 1 );
164 no warnings 'uninitialized';
165 $c->req->{path} =~ s/^($loc)?\///;
166 $c->req->{path} .= '/' if $c->req->path eq $loc;
167 my $base = $c->cgi->url;
168 if ( $ENV{CATALYST_TEST} ) {
169 my $script = $c->cgi->script_name;
170 $base =~ s/$script$//i;
172 $base = URI->new($base);
173 $base->path('/') if ( $ENV{CATALYST_TEST} || !$base->path );
174 $c->req->base( $base->as_string );
177 =item $c->prepare_request
181 sub prepare_request { shift->cgi( CGI::Simple->new ) }
183 =item $c->prepare_uploads
187 sub prepare_uploads {
189 for my $name ( $c->cgi->upload ) {
190 next unless defined $name;
191 $c->req->uploads->{$name} = {
192 fh => $c->cgi->upload($name),
193 size => $c->cgi->upload_info( $name, 'size' ),
194 type => $c->cgi->upload_info( $name, 'mime' )
199 sub run { shift->handler }
209 Sebastian Riedel, C<sri@cpan.org>
213 This program is free software, you can redistribute it and/or modify it under
214 the same terms as Perl itself.