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