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
25 This is the CGI engine for Catalyst.
31 To be called from a CGI script to start the Catalyst application.
35 This config parameter contains the C<CGI::Simple> object.
37 =head2 OVERLOADED METHODS
39 This class overloads some methods from C<Catalyst>.
41 =head3 finalize_headers
45 sub finalize_headers {
47 my %headers = ( -nph => 1 );
48 $headers{-status} = $c->response->status if $c->response->status;
49 for my $name ( $c->response->headers->header_field_names ) {
50 $headers{"-$name"} = $c->response->headers->header($name);
53 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
54 push @cookies, $c->cgi->cookie(
56 -value => $cookie->{value},
57 -expires => $cookie->{expires},
58 -domain => $cookie->{domain},
59 -path => $cookie->{path},
60 -secure => $cookie->{secure} || 0
63 $headers{-cookie} = \@cookies if @cookies;
64 print $c->cgi->header(%headers);
67 =head3 finalize_output
73 print $c->response->output;
76 =head3 prepare_cookies
80 sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) }
82 =head3 prepare_headers
88 $c->req->method( $c->cgi->request_method );
89 for my $header ( $c->cgi->http ) {
90 ( my $field = $header ) =~ s/^HTTPS?_//;
91 $c->req->headers->header( $field => $c->cgi->http($header) );
95 =head3 prepare_parameters
99 sub prepare_parameters {
101 my %vars = $c->cgi->Vars;
102 while ( my ( $key, $value ) = each %vars ) {
103 my @values = split "\0", $value;
104 $vars{$key} = @values <= 1 ? $values[0] : \@values;
106 $c->req->parameters( {%vars} );
115 $c->req->path( $c->cgi->url( -absolute => 1, -path_info => 1 ) );
116 my $loc = $c->cgi->url( -absolute => 1 );
117 no warnings 'uninitialized';
118 $c->req->{path} =~ s/^($loc)?\///;
119 $c->req->{path} .= '/' if $c->req->path eq $loc;
120 my $base = $c->cgi->url;
121 if ( $ENV{CATALYST_TEST} ) {
122 my $script = $c->cgi->script_name;
123 $base =~ s/$script$//i;
125 $base = URI->new($base);
126 $base->path('/') if ( $ENV{CATALYST_TEST} || !$base->path );
127 $c->req->base( $base->as_string );
130 =head3 prepare_request
134 sub prepare_request { shift->cgi( CGI::Simple->new ) }
136 =head3 prepare_uploads
140 sub prepare_uploads {
142 for my $name ( $c->cgi->upload ) {
143 $c->req->uploads->{$name} = {
144 fh => $c->cgi->upload($name),
145 size => $c->cgi->upload_info( $name, 'size' ),
146 type => $c->cgi->upload_info( $name, 'mime' )
151 sub run { shift->handler }
159 Sebastian Riedel, C<sri@cpan.org>
163 This program is free software, you can redistribute it and/or modify it under
164 the same terms as Perl itself.