]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC.pm
new warn function, other small trivial changes and fixes
[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 sub warn {
105   my ($self,@args) = @_;
106   my ($package, $line) = (caller)[0,2];
107   if ( $args[0] and ref $self ) {
108     $self->{ar}->warn("[$package line $line] ", @args) ;
109   } else {
110     print "warn called by ", caller, " with ", @_, "\n";
111   }
112   return;
113 }
114
115 =item parse_location
116
117 =cut
118
119 sub parse_location {
120     my $self = shift;
121
122     # Reconstruct the request headers
123     $self->headers_in(Maypole::Headers->new);
124     my %headers;
125     if ($MODPERL2) { %headers = %{$self->ar->headers_in};
126     } else { %headers = $self->ar->headers_in; }
127     for (keys %headers) {
128         $self->headers_in->set($_, $headers{$_});
129     }
130     my $path = $self->ar->uri;
131     my $loc  = $self->ar->location;
132     {
133         no warnings 'uninitialized';
134         $path .= '/' if $path eq $loc;
135         $path =~ s/^($loc)?\///;
136     }
137     $self->path($path);
138     $self->parse_path;
139     $self->parse_args;
140 }
141
142 =item parse_args
143
144 =cut
145
146 sub parse_args {
147     my $self = shift;
148     $self->params( { $self->_mod_perl_args( $self->ar ) } );
149     $self->query( $self->params );
150 }
151
152 =item redirect_request
153
154 =cut
155
156 sub redirect_request
157 {
158   my $r = shift;
159   my $redirect_url = $_[0];
160   my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' :
161           eval 'Apache::Constants::REDIRECT;'; # why have to eval this?
162   if ($_[1]) {
163     my %args = @_;
164     if ($args{url}) {
165       $redirect_url = $args{url};
166     } else {
167       my $path = $args{path} || $r->path;
168       my $host = $args{domain} || $r->ar->hostname;
169       my $protocol = $args{protocol} || $r->get_protocol;
170       $redirect_url = "${protocol}://${host}/${path}";
171     }
172     $status = $args{status} if ($args{status});
173   }
174
175   $r->ar->status($status);
176   $r->ar->headers_out->set('Location' => $redirect_url);
177   return OK;
178 }
179
180 =item get_protocol
181
182 =cut
183
184 sub get_protocol {
185   my $self = shift;
186   my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
187   return $protocol;
188 }
189
190 =item send_output
191
192 =cut
193
194 sub send_output {
195     my $r = shift;
196     $r->ar->content_type(
197           $r->content_type =~ m/^text/
198         ? $r->content_type . "; charset=" . $r->document_encoding
199         : $r->content_type
200     );
201     $r->ar->headers_out->set(
202         "Content-Length" => do { use bytes; length $r->output }
203     );
204
205     foreach ($r->headers_out->field_names) {
206         next if /^Content-(Type|Length)/;
207         $r->ar->headers_out->set($_ => $r->headers_out->get($_));
208     }
209
210     $MODPERL2 || $r->ar->send_http_header;
211     $r->ar->print( $r->output );
212 }
213
214 =item get_template_root
215
216 =cut
217
218 sub get_template_root {
219     my $r = shift;
220     $r->ar->document_root . "/" . $r->ar->location;
221 }
222
223 =back
224
225 =cut
226
227 #########################################################
228 # private / internal methods and subs
229
230
231 sub _mod_perl_args {
232     my ( $self, $apr ) = @_;
233     my %args;
234     if ($apr->isa('Apache::Request')) {
235       foreach my $key ( $apr->param ) {
236         my @values = $apr->param($key);
237         $args{$key} = @values == 1 ? $values[0] : \@values;
238       }
239     } else {
240       my $body = $self->_prepare_body($apr);
241       %args = %{$body->param};
242       my $uri = URI->new($self->ar->unparsed_uri);
243       foreach my $key ($uri->query_param) {
244         if (ref $args{$key}) {
245           push (@{$args{$key}}, $uri->query_param($key));
246         } else {
247           if ($args{$key}) {
248             $args{$key} = [ $args{$key}, $uri->query_param($key) ];
249           } else {
250             my @args = $uri->query_param($key);
251             if (scalar @args > 1) {
252               $args{$key} = [ $uri->query_param($key) ];
253             } else {
254               $args{$key} = $uri->query_param($key);
255             }
256           }
257         }
258       }
259     }
260     return %args;
261 }
262
263 sub _prepare_body {
264     my ( $self, $r ) = @_;
265
266     unless ($self->{__http_body}) {
267         my $content_type   = $r->headers_in->get('Content-Type');
268         my $content_length = $r->headers_in->get('Content-Length');
269         my $body   = HTTP::Body->new( $content_type, $content_length );
270         my $length = $content_length;
271         while ( $length ) {
272             $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
273             $length -= length($buffer);
274             $body->add($buffer);
275         }
276         $self->{__http_body} = $body;
277     }
278     return $self->{__http_body};
279 }
280
281
282
283 =head1 AUTHOR
284
285 Simon Cozens, C<simon@cpan.org>
286
287 =head1 CREDITS
288
289 Aaron Trevena
290 Marcus Ramberg, C<marcus@thefeed.no>
291 Sebastian Riedel, C<sri@oook.de>
292
293 =head1 LICENSE
294
295 You may distribute this code under the same terms as Perl itself.
296
297 =cut
298
299 1;