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