Fixed $c->req->base to be consistent in all engines, trailing slash
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Apache.pm
1 package Catalyst::Engine::Apache;
2
3 use strict;
4 use base 'Catalyst::Engine';
5
6 use URI;
7 use URI::http;
8
9 __PACKAGE__->mk_accessors(qw/apache/);
10
11 =head1 NAME
12
13 Catalyst::Engine::Apache - Catalyst Apache Engine
14
15 =head1 SYNOPSIS
16
17 See L<Catalyst>.
18
19 =head1 DESCRIPTION
20
21 This is a base class engine specialized for Apache (i.e. for mod_perl).
22
23 =head1 METHODS
24
25 =over 4
26
27 =item $c->apache
28
29 Returns an C<Apache::Request> object.
30
31 =back
32
33 =head1 OVERLOADED METHODS
34
35 This class overloads some methods from C<Catalyst::Engine>.
36
37 =over 4
38
39 =item $c->finalize_body
40
41 =cut
42
43 sub finalize_body {
44     my $c = shift;
45     $c->apache->print( $c->response->body );
46 }
47
48 =item $c->prepare_body
49
50 =cut
51
52 sub prepare_body {
53     my $c = shift;
54
55     my $length = $c->request->content_length;
56     my ( $buffer, $content );
57
58     while ($length) {
59
60         $c->apache->read( $buffer, ( $length < 8192 ) ? $length : 8192 );
61
62         $length  -= length($buffer);
63         $content .= $buffer;
64     }
65     
66     $c->request->body($content);
67 }
68
69 =item $c->prepare_connection
70
71 =cut
72
73 sub prepare_connection {
74     my $c = shift;
75     $c->request->address( $c->apache->connection->remote_ip );
76     $c->request->hostname( $c->apache->connection->remote_host );
77     $c->request->protocol( $c->apache->protocol );
78     
79     if ( $ENV{HTTPS} || $c->apache->get_server_port == 443 ) {
80         $c->request->secure(1);
81     }
82 }
83
84 =item $c->prepare_headers
85
86 =cut
87
88 sub prepare_headers {
89     my $c = shift;
90     $c->request->method( $c->apache->method );
91     $c->request->header( %{ $c->apache->headers_in } );
92 }
93
94 =item $c->prepare_parameters
95
96 =cut
97
98 sub prepare_parameters {
99     my $c = shift;
100
101     my @params;
102     
103     $c->apache->param->do( sub {
104         my ( $field, $value ) = @_;
105         push( @params, $field, $value );
106         return 1;    
107     });
108     
109     $c->request->param(@params);
110 }
111
112 =item $c->prepare_path
113
114 =cut
115
116 # XXX needs fixing, only work with <Location> directive,
117 # not <Directory> directive
118 sub prepare_path {
119     my $c = shift;
120     
121     my $base;
122     {
123         my $scheme = $c->request->secure ? 'https' : 'http';
124         my $host   = $c->apache->hostname;
125         my $port   = $c->apache->get_server_port;
126         my $path   = $c->apache->location || '/';
127         
128         unless ( $path =~ /\/$/ ) {
129             $path .= '/';
130         }
131
132         $base = URI->new;
133         $base->scheme($scheme);
134         $base->host($host);
135         $base->port($port);
136         $base->path($path);
137
138         $base = $base->canonical->as_string;
139     }
140     
141     my $location = $c->apache->location || '/';
142     my $path = $c->apache->uri || '/';
143     $path =~ s/^($location)?\///;
144     $path =~ s/^\///;
145
146     $c->req->base($base);
147     $c->req->path($path);
148 }
149
150 =item $c->run
151
152 =cut
153
154 sub run { }
155
156 =back
157
158 =head1 SEE ALSO
159
160 L<Catalyst>.
161
162 =head1 AUTHOR
163
164 Sebastian Riedel, C<sri@cpan.org>
165 Christian Hansen C<ch@ngmedia.com>
166
167 =head1 COPYRIGHT
168
169 This program is free software, you can redistribute it and/or modify it under
170 the same terms as Perl itself.
171
172 =cut
173
174 1;