]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC.pm
fix to bug in Apache::MVC location handling
[maypole.git] / lib / Apache / MVC.pm
1 package Apache::MVC;
2
3 our $VERSION = '2.11';
4
5 use strict;
6 use warnings;
7
8 use URI;
9 use URI::QueryParam;
10
11 use base 'Maypole';
12 use Maypole::Headers;
13 use Maypole::Constants;
14
15 __PACKAGE__->mk_accessors( qw( ar ) );
16
17 our $MODPERL2;
18 our $modperl_version;
19
20 BEGIN {
21     $MODPERL2  = ( exists $ENV{MOD_PERL_API_VERSION} and
22                         $ENV{MOD_PERL_API_VERSION} >= 2 );
23     if ($MODPERL2) {
24      eval 'use mod_perl2; $modperl_version = $mod_perl2::VERSION;';
25      if ($@) {
26       $modperl_version = $Apache2::RequestRec::VERSION;
27      }
28      require Apache2::RequestIO;
29      require Apache2::RequestRec;
30      require Apache2::RequestUtil;
31      eval 'use Apache2::Const -compile => qw/REDIRECT/;'; # -compile 4 no import
32      require APR::URI;
33      require HTTP::Body;
34     } else {
35      eval ' use mod_perl; ';
36      require Apache;
37      require Apache::Request;
38      eval 'use Apache::Constants -compile => qw/REDIRECT/;';
39      $modperl_version = 1;
40     }
41
42 }
43
44 =head1 NAME
45
46 Apache::MVC - Apache front-end to Maypole
47
48 =head1 SYNOPSIS
49
50     package BeerDB;
51     use Maypole::Application;
52
53 =head1 DESCRIPTION
54
55 A mod_perl platform driver for Maypole. Your application can inherit from
56 Apache::MVC directly, but it is recommended that you use
57 L<Maypole::Application>.
58
59 =head1 INSTALLATION
60
61 Create a driver module like the one illustrated in L<Maypole::Application>.
62
63 Put the following in your Apache config:
64
65     <Location /beer>
66         SetHandler perl-script
67         PerlHandler BeerDB
68     </Location>
69
70 Copy the templates found in F<templates/factory> into the F<beer/factory>
71 directory off the web root. When the designers get back to you with custom
72 templates, they are to go in F<beer/custom>. If you need to override templates
73 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
74
75 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
76 commands; for instance, to see a list of breweries, go to
77
78     http://your.site/beer/brewery/list
79
80 For more information about how the system works and how to extend it,
81 see L<Maypole>.
82
83 =head1 Implementation
84
85 This class overrides a set of methods in the base Maypole class to provide its
86 functionality. See L<Maypole> for these:
87
88 =over
89
90 =item get_request
91
92 =cut
93
94 sub get_request {
95     my ($self, $r) = @_;
96     my $ar;
97     if ($MODPERL2) {
98         $ar = eval {require Apache2::Request} ? Apache2::Request->new($r) : $r;
99         }
100     else { $ar = Apache::Request->instance($r); }
101     $self->ar($ar);
102 }
103
104 =item warn
105
106 =cut
107
108 sub warn {
109   my ($self,@args) = @_;
110   my ($package, $line) = (caller)[0,2];
111   if ( $args[0] and ref $self ) {
112     $self->{ar}->warn("[$package line $line] ", @args) ;
113   } else {
114     print "warn called by ", caller, " with ", @_, "\n";
115   }
116   return;
117 }
118
119
120 =item parse_location
121
122 =cut
123
124 sub parse_location {
125     my $self = shift;
126
127     # Reconstruct the request headers
128     $self->headers_in(Maypole::Headers->new);
129     my %headers;
130     if ($MODPERL2) { %headers = %{$self->ar->headers_in};
131     } else { %headers = $self->ar->headers_in; }
132     for (keys %headers) {
133         $self->headers_in->set($_, $headers{$_});
134     }
135
136     my $path = $self->ar->uri;
137     my $loc  = $self->ar->location;
138
139     {
140         no warnings 'uninitialized';
141         $path .= '/' if $path eq $loc;
142         if ($loc =~ /\/$/) {
143           $path =~ s/^($loc)?//;
144         } else {
145           $path =~ s/^($loc)?\///;
146         }
147     }
148
149     $self->path($path);
150     $self->parse_path;
151     $self->parse_args;
152 }
153
154 =item parse_args
155
156 =cut
157
158 sub parse_args {
159     my $self = shift;
160     $self->params( { $self->_mod_perl_args( $self->ar ) } );
161     $self->query( $self->params );
162 }
163
164 =item redirect_request
165
166 =cut
167
168 sub redirect_request
169 {
170   my $r = shift;
171   my $redirect_url = $_[0];
172   my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' :
173           eval 'Apache::Constants::REDIRECT;'; # why have to eval this?
174   if ($_[1]) {
175     my %args = @_;
176     if ($args{url}) {
177       $redirect_url = $args{url};
178     } else {
179       my $path = $args{path} || $r->path;
180       my $host = $args{domain} || $r->ar->hostname;
181       my $protocol = $args{protocol} || $r->get_protocol;
182       $redirect_url = "${protocol}://${host}/${path}";
183     }
184     $status = $args{status} if ($args{status});
185   }
186
187   $r->ar->status($status);
188   $r->ar->headers_out->set('Location' => $redirect_url);
189   return OK;
190 }
191
192 =item get_protocol
193
194 =cut
195
196 sub get_protocol {
197   my $self = shift;
198   my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
199   return $protocol;
200 }
201
202 =item send_output
203
204 =cut
205
206 sub send_output {
207     my $r = shift;
208     $r->ar->content_type(
209           $r->content_type =~ m/^text/
210         ? $r->content_type . "; charset=" . $r->document_encoding
211         : $r->content_type
212     );
213     $r->ar->headers_out->set(
214         "Content-Length" => do { use bytes; length $r->output }
215     );
216
217     foreach ($r->headers_out->field_names) {
218         next if /^Content-(Type|Length)/;
219         $r->ar->headers_out->set($_ => $r->headers_out->get($_));
220     }
221
222     $MODPERL2 || $r->ar->send_http_header;
223     $r->ar->print( $r->output );
224 }
225
226 =item get_template_root
227
228 =cut
229
230 sub get_template_root {
231     my $r = shift;
232     $r->ar->document_root . "/" . $r->ar->location;
233 }
234
235 =back
236
237 =cut
238
239 #########################################################
240 # private / internal methods and subs
241
242
243 sub _mod_perl_args {
244     my ( $self, $apr ) = @_;
245     my %args;
246     if ($apr->isa('Apache::Request')) {
247       foreach my $key ( $apr->param ) {
248         my @values = $apr->param($key);
249         $args{$key} = @values == 1 ? $values[0] : \@values;
250       }
251     } else {
252       my $body = $self->_prepare_body($apr);
253       %args = %{$body->param};
254       my $uri = URI->new($self->ar->unparsed_uri);
255       foreach my $key ($uri->query_param) {
256         if (ref $args{$key}) {
257           push (@{$args{$key}}, $uri->query_param($key));
258         } else {
259           if ($args{$key}) {
260             $args{$key} = [ $args{$key}, $uri->query_param($key) ];
261           } else {
262             my @args = $uri->query_param($key);
263             if (scalar @args > 1) {
264               $args{$key} = [ $uri->query_param($key) ];
265             } else {
266               $args{$key} = $uri->query_param($key);
267             }
268           }
269         }
270       }
271     }
272     return %args;
273 }
274
275 sub _prepare_body {
276     my ( $self, $r ) = @_;
277
278     unless ($self->{__http_body}) {
279         my $content_type   = $r->headers_in->get('Content-Type');
280         my $content_length = $r->headers_in->get('Content-Length');
281         my $body   = HTTP::Body->new( $content_type, $content_length );
282         my $length = $content_length;
283         while ( $length ) {
284             $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
285             $length -= length($buffer);
286             $body->add($buffer);
287         }
288         $self->{__http_body} = $body;
289     }
290     return $self->{__http_body};
291 }
292
293
294
295 =head1 AUTHOR
296
297 Simon Cozens, C<simon@cpan.org>
298
299 =head1 CREDITS
300
301 Aaron Trevena
302 Marcus Ramberg, C<marcus@thefeed.no>
303 Sebastian Riedel, C<sri@oook.de>
304
305 =head1 LICENSE
306
307 You may distribute this code under the same terms as Perl itself.
308
309 =cut
310
311 1;