fixed isa tree
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
3use strict;
4use base 'Catalyst::Engine';
5use URI;
6
7require CGI::Simple;
8require CGI::Cookie;
9
7833fdfc 10$CGI::Simple::POST_MAX = 1048576;
11$CGI::Simple::DISABLE_UPLOADS = 0;
12
fc7ec1d9 13__PACKAGE__->mk_accessors('cgi');
14
15=head1 NAME
16
17Catalyst::Engine::CGI - The CGI Engine
18
19=head1 SYNOPSIS
20
9a33da6a 21 #!/usr/bin/perl -w
22
23 use strict;
24 use lib '/path/to/MyApp/lib';
25 use MyApp;
26
27 MyApp->run;
28
fc7ec1d9 29See L<Catalyst>.
30
31=head1 DESCRIPTION
32
33This is the CGI engine for Catalyst.
34
9a33da6a 35The script shown above must be designated as a "Non-parsed Headers"
36script to function properly.
37To do this in Apache name the script starting with C<nph->.
38
39The performance of this way of using Catalyst is not expected to be
40useful in production applications, but it may be helpful for development.
41
fc7ec1d9 42=head2 METHODS
43
44=head3 run
45
46To be called from a CGI script to start the Catalyst application.
47
48=head3 cgi
49
50This config parameter contains the C<CGI::Simple> object.
51
52=head2 OVERLOADED METHODS
53
54This class overloads some methods from C<Catalyst>.
55
56=head3 finalize_headers
57
58=cut
59
60sub finalize_headers {
61 my $c = shift;
62 my %headers = ( -nph => 1 );
63 $headers{-status} = $c->response->status if $c->response->status;
64 for my $name ( $c->response->headers->header_field_names ) {
65 $headers{"-$name"} = $c->response->headers->header($name);
66 }
67 my @cookies;
68 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
69 push @cookies, $c->cgi->cookie(
70 -name => $name,
71 -value => $cookie->{value},
72 -expires => $cookie->{expires},
73 -domain => $cookie->{domain},
74 -path => $cookie->{path},
75 -secure => $cookie->{secure} || 0
76 );
77 }
78 $headers{-cookie} = \@cookies if @cookies;
79 print $c->cgi->header(%headers);
80}
81
82=head3 finalize_output
83
84=cut
85
86sub finalize_output {
87 my $c = shift;
88 print $c->response->output;
89}
90
91=head3 prepare_cookies
92
93=cut
94
95sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) }
96
97=head3 prepare_headers
98
99=cut
100
101sub prepare_headers {
102 my $c = shift;
103 $c->req->method( $c->cgi->request_method );
104 for my $header ( $c->cgi->http ) {
105 ( my $field = $header ) =~ s/^HTTPS?_//;
106 $c->req->headers->header( $field => $c->cgi->http($header) );
107 }
108}
109
110=head3 prepare_parameters
111
112=cut
113
114sub prepare_parameters {
115 my $c = shift;
116 my %vars = $c->cgi->Vars;
117 while ( my ( $key, $value ) = each %vars ) {
118 my @values = split "\0", $value;
119 $vars{$key} = @values <= 1 ? $values[0] : \@values;
120 }
121 $c->req->parameters( {%vars} );
122}
123
124=head3 prepare_path
125
126=cut
127
128sub prepare_path {
129 my $c = shift;
130 $c->req->path( $c->cgi->url( -absolute => 1, -path_info => 1 ) );
131 my $loc = $c->cgi->url( -absolute => 1 );
132 no warnings 'uninitialized';
133 $c->req->{path} =~ s/^($loc)?\///;
134 $c->req->{path} .= '/' if $c->req->path eq $loc;
135 my $base = $c->cgi->url;
7833fdfc 136 if ( $ENV{CATALYST_TEST} ) {
137 my $script = $c->cgi->script_name;
138 $base =~ s/$script$//i;
139 }
fc7ec1d9 140 $base = URI->new($base);
141 $base->path('/') if ( $ENV{CATALYST_TEST} || !$base->path );
142 $c->req->base( $base->as_string );
143}
144
145=head3 prepare_request
146
147=cut
148
149sub prepare_request { shift->cgi( CGI::Simple->new ) }
150
151=head3 prepare_uploads
152
153=cut
154
155sub prepare_uploads {
156 my $c = shift;
157 for my $name ( $c->cgi->upload ) {
fc7ec1d9 158 $c->req->uploads->{$name} = {
7833fdfc 159 fh => $c->cgi->upload($name),
160 size => $c->cgi->upload_info( $name, 'size' ),
161 type => $c->cgi->upload_info( $name, 'mime' )
fc7ec1d9 162 };
163 }
164}
165
166sub run { shift->handler }
167
168=head1 SEE ALSO
169
170L<Catalyst>.
171
172=head1 AUTHOR
173
174Sebastian Riedel, C<sri@cpan.org>
175
176=head1 COPYRIGHT
177
178This program is free software, you can redistribute it and/or modify it under
179the same terms as Perl itself.
180
181=cut
182
1831;