Added recursive -r flag to prove example
[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} );
66294129 72 $c->request->user( $ENV{REMOTE_USER} );
c2e8e6fa 73
b7b011a9 74 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
75 $c->request->secure(1);
76 }
77
78 if ( $ENV{SERVER_PORT} == 443 ) {
c2e8e6fa 79 $c->request->secure(1);
80 }
81}
82
83=item $c->prepare_headers
84
85=cut
86
87sub prepare_headers {
88 my $c = shift;
89
90 while ( my ( $header, $value ) = each %ENV ) {
91
92 next unless $header =~ /^(HTTP|CONTENT)/i;
93
94 ( my $field = $header ) =~ s/^HTTPS?_//;
95
96 $c->req->headers->header( $field => $value );
97 }
98
99 $c->req->method( $ENV{REQUEST_METHOD} || 'GET' );
100}
101
102=item $c->prepare_path
103
104=cut
105
106sub prepare_path {
107 my $c = shift;
108
109 my $base;
110 {
111 my $scheme = $c->request->secure ? 'https' : 'http';
112 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
113 my $port = $ENV{SERVER_PORT} || 80;
114 my $path = $ENV{SCRIPT_NAME} || '/';
115
116 unless ( $path =~ /\/$/ ) {
117 $path .= '/';
118 }
119
120 $base = URI->new;
121 $base->scheme($scheme);
122 $base->host($host);
123 $base->port($port);
124 $base->path($path);
125
126 $base = $base->canonical->as_string;
127 }
128
316bf0f0 129 my $location = $ENV{SCRIPT_NAME} || '/';
c2e8e6fa 130 my $path = $ENV{PATH_INFO} || '/';
316bf0f0 131 $path =~ s/^($location)?\///;
c2e8e6fa 132 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
133 $path =~ s/^\///;
134
135 $c->req->base($base);
136 $c->req->path($path);
137}
138
139=item $c->run
140
141=cut
142
fc88e495 143sub run { shift->handler(@_) }
c2e8e6fa 144
145=back
146
147=head1 SEE ALSO
148
149L<Catalyst>.
150
151=head1 AUTHOR
152
153Sebastian Riedel, C<sri@cpan.org>
154Christian Hansen, C<ch@ngmedia.com>
155
156=head1 COPYRIGHT
157
158This program is free software, you can redistribute it and/or modify it under
159the same terms as Perl itself.
160
161=cut
162
1631;