Fixed MP2, removed dependency of libapreq in MP engines, fixed C::E::C::APR
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI / Base.pm
CommitLineData
c2e8e6fa 1package Catalyst::Engine::CGI::Base;
2
3use strict;
4use base 'Catalyst::Engine';
5
6use URI;
7use URI::http;
8
c2e8e6fa 9=head1 NAME
10
11Catalyst::Engine::CGI::Base - Base class for CGI Engines
12
13=head1 DESCRIPTION
14
15This is a base class for CGI engines.
16
c2e8e6fa 17=head1 OVERLOADED METHODS
18
19This class overloads some methods from C<Catalyst::Engine>.
20
21=over 4
22
23=item $c->finalize_body
24
25Prints the response output to STDOUT.
26
27=cut
28
29sub finalize_body {
30 my $c = shift;
31 print $c->response->output;
32}
33
34=item $c->finalize_headers
35
36=cut
37
38sub finalize_headers {
39 my $c = shift;
40
41 $c->response->header( Status => $c->response->status );
42
43 print $c->response->headers->as_string("\015\012");
44 print "\015\012";
45}
46
e05d67cf 47=item $c->prepare_body
48
49=cut
50
51sub prepare_body {
52 my $c = shift;
53
e2fd5b5f 54 my $body = undef;
e05d67cf 55
e2fd5b5f 56 while ( read( STDIN, my $buffer, 8192 ) ) {
e05d67cf 57 $body .= $buffer;
58 }
59
60 $c->request->body($body);
61}
62
c2e8e6fa 63=item $c->prepare_connection
64
65=cut
66
67sub prepare_connection {
68 my $c = shift;
69 $c->request->address( $ENV{REMOTE_ADDR} );
70 $c->request->hostname( $ENV{REMOTE_HOST} );
71 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
72
73 if ( $ENV{HTTPS} || $ENV{SERVER_PORT} == 443 ) {
74 $c->request->secure(1);
75 }
76}
77
78=item $c->prepare_headers
79
80=cut
81
82sub prepare_headers {
83 my $c = shift;
84
85 while ( my ( $header, $value ) = each %ENV ) {
86
87 next unless $header =~ /^(HTTP|CONTENT)/i;
88
89 ( my $field = $header ) =~ s/^HTTPS?_//;
90
91 $c->req->headers->header( $field => $value );
92 }
93
94 $c->req->method( $ENV{REQUEST_METHOD} || 'GET' );
95}
96
97=item $c->prepare_path
98
99=cut
100
101sub prepare_path {
102 my $c = shift;
103
104 my $base;
105 {
106 my $scheme = $c->request->secure ? 'https' : 'http';
107 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
108 my $port = $ENV{SERVER_PORT} || 80;
109 my $path = $ENV{SCRIPT_NAME} || '/';
110
111 unless ( $path =~ /\/$/ ) {
112 $path .= '/';
113 }
114
115 $base = URI->new;
116 $base->scheme($scheme);
117 $base->host($host);
118 $base->port($port);
119 $base->path($path);
120
121 $base = $base->canonical->as_string;
122 }
123
316bf0f0 124 my $location = $ENV{SCRIPT_NAME} || '/';
c2e8e6fa 125 my $path = $ENV{PATH_INFO} || '/';
316bf0f0 126 $path =~ s/^($location)?\///;
c2e8e6fa 127 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
128 $path =~ s/^\///;
129
130 $c->req->base($base);
131 $c->req->path($path);
132}
133
134=item $c->run
135
136=cut
137
138sub run { shift->handler }
139
140=back
141
142=head1 SEE ALSO
143
144L<Catalyst>.
145
146=head1 AUTHOR
147
148Sebastian Riedel, C<sri@cpan.org>
149Christian Hansen, C<ch@ngmedia.com>
150
151=head1 COPYRIGHT
152
153This program is free software, you can redistribute it and/or modify it under
154the same terms as Perl itself.
155
156=cut
157
1581;